Rows.Count Error

Lovelylou79

New Member
Joined
Sep 4, 2017
Messages
37
Hi Folks,
I am trying to get data from multiple workbooks into 1workbook, building up a large data source.
The code should open each workbook selected, copy cellsA3:AP and last row and then paste into the new workbook at the last blank row.
I am currently struggling with finding the last row to copy to. The code seems correct, and works fine in the individual workbooks, but when using it in the workbook where the data will be stored, it only copies the first 3 rows (A1:AP1 to A3:AP3).
I cant see where it is failing. Any help would be appreciated.
Code:
Private Sub CommandButton1_Click()


'Import all User data from the various AM Audit Workbooks
Dim FileNameXLs, f
Dim wb As Workbook, i As Integer
Dim lr As Integer
 
FileNameXLs =Application.GetOpenFilename(filefilter:="Excel Files, *.xl*",MultiSelect:=True)
 
If Not IsArray(FileNameXLs) Then Exit Sub
 
Application.ScreenUpdating = False
 
For Each f In FileNameXLs
Set wb = Workbooks.Open(f)
wb.Worksheets("Sheet1").Range("A3:AP"& Cells(Rows.Count, "B").End(xlUp).Row).CopyThisWorkbook.Sheets("AM_AUDIT_V1").Range("A3")
 
Application.CutCopyMode = False
wb.Close Savechanges:=False
Next f
Application.ScreenUpdating = True

 

End Sub


Thanks
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try
Code:
wb.Worksheets("Sheet1").Range("A3:AP"& 
wb.Worksheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row).Copy ThisWorkbook.Sheets("AM_AUDIT_V1").Range("A3")
 
Last edited:
Upvote 0
The code didn't compile correctly for me because of the spacing (probably because I typed it on my phone), does the below work for you? if not what does the error say?

Code:
wb.Worksheets("Sheet1").Range("A3:AP" & wb.Worksheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row).Copy ThisWorkbook.Sheets("AM_AUDIT_V1").Range("A3")
 
Upvote 0
Thanks again Mark, I did assume the line drop wasn’t supposed to be there ?.
The error I’m getting is a run time error 1004 Application-defines or object defined error.

I tried using the code to just select the data, and it only picks up the first 3 rows.
I changed the A3 to A1 and it only picked up the first row.
Can’t say I’ve ever come across this issue before.
 
Upvote 0
No protection or merged cells? (btw the extra spacing was after the &, the line drop was just how the board displayed it)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top