昨天更新此线程:Excel VBA:查找数据、循环多个工作表、复制特定单元格范围
(特别感谢 findwindow 让我走到这一步!)
我在某个部分不断收到运行时 91 错误,并最终放入 If/Then 语句以跳到下一张表...但现在我在其正下方的行上收到错误 1004(见下文):
Sub Pull_data_Click()
Dim A As Variant 'defines name from first subroutine
Dim B As Workbook 'defines destination file
Dim X As Workbook 'defines existing report file as source
Dim Destination As Range 'defines destination range of data pulled from report
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Variant
Dim copyRng As Variant
Dim fRow As Long
Application.ScreenUpdating = False
Set B = Workbooks("filenameB.xlsm") 'constant variable, does not change
Set X = Workbooks.Open("filenameX.xlsm") 'dependent variable, new name for each new report
A = B.Worksheets("Summary").Range("A1").Value 'constant variable, does not change
Set Destination = B.Worksheets("Input").Range("B2:S2") 'Range changes for each iteration, rows increase by 1
'check if name is entered
If A = "" Then
MsgBox ("Your name is not visible; please start from the Reference tab.")
B.Worksheets("Reference").Activate
Exit Sub
End If
For Each ws In X.Worksheets
With ws.range("A:A")
Set rng = .Find(What:=A, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If ring Is Nothing Then 'do nothing
Else
fRow = rng.Row
Set copyRng = ws.Range(Cells(fRow, 1), Cells(fRow, 18))
Destination = copyRng
End With
Next ws
Application.ScreenUpdating = True
End Sub
昨天,出现了错误91:
fRow = rng.Row
今天,在该区域放入 If/Then 部分后,我收到错误 1004(对象“_Worksheet”的方法“范围”失败):
设置 copyRng = ws.Range(Cells(fRow, 1), Cells(fRow, 18))
语法正在工作,并且似乎正在查找正确的工作簿,但我不确定它是否被卡住,因为我正在搜索的变量(变量 A)不存在于第一张纸上。有任何想法吗?