昨天更新此线程:Excel VBA: Find data, loop through multiple worksheets, copy specific range of cells
(特别感谢 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”的方法“范围”失败):
Set copyRng = ws.Range(Cells(fRow, 1), Cells(fRow, 18))
语法正在工作,并且似乎正在查找正确的工作簿,但我不确定它是否被卡住,因为我正在搜索的变量(变量 A)不存在于第一张纸上。有什么想法吗?
最佳答案
不确定这是否是您正在寻找的内容? 失踪了还有结局吗?您可以在一行中完成复制。见下文...
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 rng Is Nothing Then 'do nothing
Else
fRow = rng.Row
ws.Range("A" + CStr(fRow) + ":" + "R" + CStr(fRow)).Copy Destination:=Destination
End If
End With
Next ws
关于macos - Excel VBA更新: Find data,循环多个工作表,复制范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32252879/