【问题标题】:Excel VBA Run-time error '424': Object Required when trying to Save and Close a WorkbookExcel VBA 运行时错误“424”:尝试保存和关闭工作簿时需要对象
【发布时间】:2017-08-21 19:04:26
【问题描述】:

我正在循环以复制文件夹中所有文件的所有标题。当我尝试关闭文件时出现错误。这发生在 with 语句中。基本上我从我的桌面打开文件,复制标题,我无法关闭文件,因为我收到了错误 Object Required

如果您需要更多详细信息,请告诉我。提前谢谢!

Sub LoopAllExcelFilesInFolder()
Dim wb, y, export As Workbook
Dim myPath, CopyPath, CopyFile As String
Dim myFile  As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim LR      As Long
Dim rgCut   As Excel.Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
    .Title = "Select A Target Folder"
    .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With

'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"

'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
Do While myFile <> ""
    'Set variable equal to opened workbook
    Set wb = Workbooks.Open(Filename:=myPath & myFile)
    'Ensure Workbook has opened before moving on to next line of code
    DoEvents
    Set rgCut = wb.Worksheets(1).Range("A1").EntireRow
    Set y = Workbooks.Open("C:\Users\icohen\Desktop\exceltest.xlsx")
    Workbooks.Open("C:\Users\icohen\Desktop\exceltest.xlsx").Activate
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    With y
        Sheets(1).Cells.Select
        If LR = 0 Then
            rgCut.Copy Destination:=Selection.Range("A1")

        Else
            rgCut.Copy Destination:=Selection.Range("A" & LR)
            .Save            ' Here i receive the error
            .Close           ' Here i receive the error

        End If

    End With

    'Save and Close Workbook
    wb.Close SaveChanges:=True

    'Ensure Workbook has closed before moving on to next line of code
    DoEvents

    'Get next file name
    myFile = Dir
Loop

'Message Box when tasks are completed
MsgBox "Task Complete!"

ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

【问题讨论】:

  • 您是否尝试过指定 y.save 和 y.close?由于前面的语句在选择中执行命名范围剪切和粘贴,尽管有 With 语句,它可能会混淆系统。
  • 你打开同一个工作簿两次:你只需要y.Activate
  • 谢谢西里尔!我之所以喜欢这个只是因为我有其他错误:(我发现复制的方式适合这样。
  • 摆脱Sheets(1).Cells.Select ...使用rgCut.Copy Destination:=Sheets(1).Range("A1")
  • @Mat'sMug,你是绝对正确的。但是如果有人重命名工作表,那么代码将失败,而不是践踏所有工作表(1)

标签: vba excel


【解决方案1】:

您需要考虑您的内联多重声明习惯(是吗?),如:

Dim wb, y, export As Workbook

在这个变量列表中,“Export”是一个工作簿,但“wb”和“y”只是变体。如果您希望将这三个都声明为工作簿,则需要逐个变量地拼写出来;这就是 VBA 的工作原理:)

Dim wb As Workbook, y As Workbook, export As Workbook

这样做确实节省了一些空间...但我总是在它自己的代码行中进行每个声明(Dim...)--这是一个选择问题。

除此之外,它不是“Sheets”而是“.Sheets”

With y
    .Sheets(1).Cells.Select

虽然你为什么这样做我不知道...... 以下应该就可以了:

y.Sheets(1).Select
Rows("1:1").Copy
y.Sheets(2).Range("A1").Select
ActiveSheet.Paste

【讨论】:

  • 你应该更进一步。并完成y.Sheets(1).Rows("1:1").Copyy.Sheets(2).Range("A1").Paste
  • 这似乎是该方法的一个非常合乎逻辑的演变,但这是不可能的,因为 Range 对象在 VBA 中没有粘贴方法。但是,有一个 PasteSpecial 方法,它确实可以工作。 John Cohen 有一个问题需要解决,而不是想找到最优化或最简洁的解决方案。作为一种解释,我认为把它拼出来是一种更好的方法。尽管如此,还是感谢您的评论!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
相关资源
最近更新 更多