Run time 1004 Error - Defining last column in cell to copy range

Eclektics

New Member
Joined
Jun 9, 2015
Messages
24
Hi everyone,

I'm trying to use a count formula on my worksheet to tell VBA the range of columns I want to copy. This will change between different data sets. I've got the last column number on my "Home" sheet and the range to copy is on the "Calculation" sheet.

I'm not sure why the below isn't not working, I've gone through a few different threads on here to try and resolve it but to no avail. The macro stops on '.Range(Cells(2, ColNo), Cells(LRow, ColNo)).Copy'

Code:
Sub test_module()


Dim ColNo, LRow As Long


LRow = Sheets("Calculation").Cells(Sheets("Calculation").Rows.Count, "A").End(xlUp).Row


With Worksheets("Home")
    ColNo = .Range("K7").Value
End With


With Worksheets("Calculation")
    .Range(Cells(2, ColNo), Cells(LRow, ColNo)).Copy
End With


Sheets("Home").Range("P4").PasteSpecial xlPasteValues


End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try replacing that line with
Code:
.Cells(2 , ColNo).Resize(LRow - 1).Copy
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
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