【发布时间】:2020-07-14 05:51:37
【问题描述】:
我正在编写代码来为单个单元格值抓取多个工作簿并将该值导入主电子表格。我下面的代码在工作时效果很好,但我发现有几个工作簿可能由于被锁定或其他导致错误而停止代码的问题而出现问题。我想做的是使用On error resume next 命令继续从其他工作簿导入值,但我需要一种方法来记录由于错误而被跳过的工作簿,以便可以手动提取值(理想情况下主工作簿中的单独工作表)。这是我到目前为止的代码:
Sub CopyRange()
Application.ScreenUpdating = False
Dim wkbDest As Workbook, sh As Worksheet
Dim wkbSource As Workbook
Set wkbDest = ThisWorkbook
Dim LastRow As Long
Const strPath As String = "E:\Desktop\Example\"
ChDir strPath
strExtension = Dir(strPath & "*.xls*")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath & strExtension)
With wkbSource
On Error Resume Next
'locate last row to start copying new value from the next spreadsheet
LastRow = wkbDest.Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
'From the Basis & Credits cell AB46, copy to last row+1 in the Master sheet starting in row A2
.Sheets("Basis & Credits").Range("AB46").Copy
wkbDest.Sheets("Master").Range("A" & LastRow).PasteSpecial Paste:=xlPasteValues
.Close savechanges:=False
End With
strExtension = Dir
Loop
Application.ScreenUpdating = True
End Sub
【问题讨论】:
标签: excel vba error-handling