Using dynamic column numbers in VBA

Robert E Lee

Active Member
Joined
Aug 10, 2005
Messages
266
Hi

I have a monthly spreadsheet which has a formula in each column on Row 2. Each month I wish to copy this formula to the next column. I am trying to use this code

Code:
Range(Cells(2, H)).Copy Destination:=Range(Cells(2, H2), Cells(R, H2))

where H, H2 & R are dynamic numbers. This fails for some reason which I cannot see. Can someone help

Many thanks

Rob
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
just to be clear if in month 1 the amount was in column a and the formula in column e
the next month should this be column a and column f or column b and column f?
 
Upvote 0
Error message "Method 'Range' of 'object_Global' failed.

The values of H, H2 and R are 61, 62, and 1860 respectively

Rob
 
Upvote 0
The formula is in column A in Month 1 and Column B in Month 2, nott quite sure which "amount" you are referring to
 
Upvote 0
Problem solved; it turns out that I did not need the first 'Range' in the line of code

Thanks for your interest

Rob
 
Upvote 0

Forum statistics

Threads
1,206,808
Messages
6,074,983
Members
446,109
Latest member
ab3g1nner

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