gpt4 book ai didi

vba - 运行时错误 '1004' 对象 'Save' 的方法 '_Workbook' 失败

转载 作者:行者123 更新时间:2023-12-04 21:39:57 24 4
gpt4 key购买 nike

运行 VBA 应用程序时出现此错误。我认为这个错误与我的代码中的以下行有关

ActiveWorkbook.Save

这是整个代码。
LDate = Date
LDate = Mid(LDate, 4, 2)
If LDate > 8 Then
Sheets("a").Cells(13, "H").Value = Sheets("a").Cells(13, "H").Value + 1000
Else
Sheets("a").Cells(13, "H").Value = Sheets("a").Cells(13, "H").Value + 1
End If
ActiveWorkbook.Save

有人可以解释这个错误的原因以及我如何解决它。

请阅读以下评论。

这是单击第一个按钮时执行的子例程。
Sub import()
Dim Filt As String
Dim FilterIndex As Integer
Dim Title As String
Dim FileName As Variant
Dim finalrow As Integer
Dim alldata As String
Dim temp As String
Dim oFSO As New FileSystemObject
Dim oFS As TextStream

'Filt = "Cst Files (*.txt),*.txt"
'Title = "Select a cst File to Import"
'FileName = Application.GetOpenFilename(FileFilter:=Filt, Title:=Title)
'If FileName = False Then
'MsgBox "No File Was Selected"
'Exit Sub
'End If

'Call TestReference

' Open the file dialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
If diaFolder.SelectedItems.Count <> 0 Then
folderpath = diaFolder.SelectedItems(1)
folderpath = folderpath & "\"
'MsgBox diaFolder.SelectedItems(1)

Set diaFolder = Nothing

'RefreshSheet
On Error Resume Next
temp = folderpath & "*.txt"
sFile = Dir(temp)

Do Until sFile = ""
inputRow = Sheets("RawData").Range("A" & Rows.Count).End(xlUp).Row + 1
FileName = folderpath & sFile
Set oFS = oFSO.OpenTextFile(FileName)
Dim content As String
content = oFS.ReadAll
content = Mid(content, 4, Len(content) - 3)
With Sheets("RawData").Range("A" & inputRow)
.NumberFormat = "@"
.Value = content
End With

oFS.Close
Set oFS = Nothing

alldata = ""
finalrow = Sheets("RawData").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("RawData").Activate
For i = inputRow To finalrow
alldata = alldata & Cells(i, "A").Value & " "
Cells(i, "A").Value = ""
Next i
Cells(inputRow, "B").Value = alldata

temp = StrReverse(FileName)
temp = Left(temp, InStr(1, temp, "\") - 1)
temp = StrReverse(temp)
temp = Left(temp, InStr(1, temp, ".") - 1)
Cells(inputRow, "A").Value = temp
Sheets("RawData").Cells(inputRow, "A").NumberFormat = "@"
sFile = Dir()
Loop
Else
MsgBox ("No Folder Selected")
End If
End Sub

执行此宏后如何使此代码停止访问工作表?

最佳答案

虽然我认为你应该 认真考虑重构你的代码 ,您应该首先引用由 .Save() 方法调用的正确工作簿。

    Workbooks("Insert_Workbook_Name_Here.xlsm").Save

确保工作簿名称和扩展名(.xlsm、.xls、.xlsx)与您实际尝试保存的文件匹配。

关于vba - 运行时错误 '1004' 对象 'Save' 的方法 '_Workbook' 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18426950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com