Update to this thread from yesterday: Excel VBA: Find data, loop through multiple worksheets, copy specific range of cells
(Special thanks to findwindow for getting me this far!)
I kept getting a runtime 91 error on a certain section, and eventually put in an If/Then statement to skip to the next sheet...but now I'm getting an error 1004 on the line right below it (see below):
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
Yesterday, the error 91 occurred on this:
fRow = rng.Row
Today, after I put in the If/Then section in that area, I'm getting error 1004 (Method 'Range' of object "_Worksheet' failed) on:
Set copyRng = ws.Range(Cells(fRow, 1), Cells(fRow, 18))
The syntax is working and it seems to be looking in the correct workbook, but I'm not sure if it's getting stuck because the variable I'm searching for (Variable A) isn't present on the first sheet. Any ideas?