Texas Longhorn
Active Member
- Joined
- Sep 30, 2003
- Messages
- 496
Hi All,
I am trying to write a macro that will cycle through each sheet in a source workbook (Historicals 2007_2011.xlsx), copy a range within each of those sheets, and paste values into a destination workbook (CombinedDataHist_20250318.xlsm). The source workbook, which is open while I am running the macro, has 60 worksheets. Using the first sheet as an example, I would like to copy the range from cell A15 to the cell in the last row of column EM (e.g., A15:EM1500). In the destination workbook, I would like to paste values. The first data block would be pasted in cell A2 of the destination workbook. When the macro cycles to the next sheet in the source workbook and returns to the destination workbook, the next block of data will be pasted below the previous block (in this example, cell A1488).
I have pasted my code below, which currently fails with a Run-time error '1004': Select method of Range class failed. When I enter the debugger, the line ws.Range("A10000").Select is highlighted. Unfortunately, my VBA is very rusty. I would greatly appreciate it if anyone could offer suggestions that (1) get my code working; and (2) help me clean up less-than-elegant lines like the aforementioned and Range("A1000000").Select.
Thank you.
I am trying to write a macro that will cycle through each sheet in a source workbook (Historicals 2007_2011.xlsx), copy a range within each of those sheets, and paste values into a destination workbook (CombinedDataHist_20250318.xlsm). The source workbook, which is open while I am running the macro, has 60 worksheets. Using the first sheet as an example, I would like to copy the range from cell A15 to the cell in the last row of column EM (e.g., A15:EM1500). In the destination workbook, I would like to paste values. The first data block would be pasted in cell A2 of the destination workbook. When the macro cycles to the next sheet in the source workbook and returns to the destination workbook, the next block of data will be pasted below the previous block (in this example, cell A1488).
I have pasted my code below, which currently fails with a Run-time error '1004': Select method of Range class failed. When I enter the debugger, the line ws.Range("A10000").Select is highlighted. Unfortunately, my VBA is very rusty. I would greatly appreciate it if anyone could offer suggestions that (1) get my code working; and (2) help me clean up less-than-elegant lines like the aforementioned and Range("A1000000").Select.
Thank you.
VBA Code:
Sub CopyTabs()
Dim wb As Workbook
Dim ws As Worksheet
Dim LastRow As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Set wb = Workbooks("Historicals 2007_2011.xlsx")
For Each ws In wb.Worksheets
ws.Range("A10000").Select
LastRow = ws.Range("A:A").SpecialCells(xlCellTypeLastCell).Row
Range("A15:EM" & LastRow).Copy
Windows("CombinedDataHist_20250318.xlsm").Activate
Range("A1000000").Select
Selection.End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Next ws
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub