VBA 1004 Error when using a Variable in the Range

ConestogaKid

New Member
Joined
Feb 10, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am trying to see if it is possible to paste specific data below the data on another sheet. I am sure I have no clue how to correctly use select and activesheet and they probably slow down all of my code.
If you have any tips on how to get this to work correctly or just write it better in general. When I use a specific range, ("A786" is listed as the one currently) I don't have any errors and it works just fine, however when I try to use Cell(last_row2, 1) it gives the 1004 errors.
Sheet to copy data from: CLA_MT (CA)
Sheet to paste data to: SalesData

If you need any other information please let me know.

Sub CLAMT_Copy()
Dim last_row1, last_row2 As Long

Sheets("CLA_MT (CA)").Select
ActiveSheet.Name = "CLA_MT (CA)"

'Get last row CLA_MT (CA)
last_row1 = Cells(Rows.Count, 1).End(xlUp).Row

Sheets("SalesData").Select
ActiveSheet.Name = "SalesData"

'get last row in SalesData to paste info below it
last_row2 = Cells(Rows.Count, 1).End(xlUp).Row
last_row2 = last_row2 + 1
'Cells(786, 1) = last_row2 'confirming pasting in the correct row

Sheets("CLA_MT (CA)").Select
ActiveSheet.Name = "CLA_MT (CA)"

'copy data reformatted data from CLA_MT
Worksheets("CLA_MT (CA)").Range(Cells(2, 15), Cells(last_row1, 24)).Copy

'Pastes to the end of the SalesData Sheet
Worksheets("SalesData").Range(Cells(last_row2, 1)).PasteSpecial Paste:=xlPasteValues
'Worksheets("SalesData").Range("A786").PasteSpecial Paste:=xlPasteValues 'this works but i need to set it to a variable range to adjust for new data
Application.CutCopyMode = False

End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi & welcome to MrExcel.
How about
VBA Code:
Sub CLAMT_Copy()
   Dim last_row1 As Long
   
   With Sheets("CLA_MT (CA)")
      last_row1 = .Cells(Rows.count, 1).End(xlUp).Row
   'copy data reformatted data from CLA_MT
      .Range(.Cells(2, 15), Cells(last_row1, 24)).Copy
   End With
   'Pastes to the end of the SalesData Sheet
   With Worksheets("SalesData")
      .Range("A" & Rows.count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
   End With
End Sub
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
Sub CLAMT_Copy()
   Dim last_row1 As Long
  
   With Sheets("CLA_MT (CA)")
      last_row1 = .Cells(Rows.count, 1).End(xlUp).Row
   'copy data reformatted data from CLA_MT
      .Range(.Cells(2, 15), Cells(last_row1, 24)).Copy
   End With
   'Pastes to the end of the SalesData Sheet
   With Worksheets("SalesData")
      .Range("A" & Rows.count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
   End With
End Sub
That worked! thank you so much! I spent way too long trying to do this. I dont quite understand your code yet, but I will look into the offset and how you used ranges and the with sheets.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,392
Messages
6,119,257
Members
448,880
Latest member
aveternik

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