Macro to copy Specific Cols from one sheet to another

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have written the following code to copy data from row 1 to the last row containing data in Cols O, Q:U

However I get application defined or object defined error


Code:
 sheets(2).Range("O1:O", "Q:U" & LR).Copy Destination:=Sheets(3).Range("A1")


It would be appreciated if someone could kindly amend my code






Code:
 Sub Copy_Sales_Values()
Sheets(2).Select
Dim LR As Long

LR = Range("A" & Rows.Count).End(xlUp).Row
Sheets(3).Select
Range("A1:K" & LR).ClearContents

Sheets(2).Range("O1:O", "Q:U" & LR).Copy Destination:=Sheets(3).Range("A1")

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
One simple way is just to copy 2 ranges. I have made some other changes/guesses with the code below so make sure you test in a copy of your workbook.

Code:
Sub Copy_Sales_Values()
  Dim LR As Long
  
  Sheets(3).UsedRange.ClearContents
  With Sheets(2)
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("O1:O" & LR).Copy Destination:=Sheets(3).Range("A1")
    .Range("Q1:U" & LR).Copy Destination:=Sheets(3).Range("B1")
  End With
End Sub
 
Upvote 0
Thanks for the help Peter
 
Last edited:
Upvote 0
With Peter's assumptions, this macro should also work...
Code:
[table="width: 500"]
[tr]
	[td]Sub Copy_Sales_Values()
  Dim LR As Long
  Sheets(3).UsedRange.ClearContents
  LR = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row
  Sheets(3).Range("A1:F" & LR) = Application.Index(Sheets(2).Cells, Evaluate("ROW(1:" & LR & ")"), Split("15 17 18 19 20 21"))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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