Copy cell only as far as the previous column cells go down in Macro

Peter Davison

Active Member
Joined
Jun 4, 2020
Messages
433
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a cell F1 which I want to copy in a macro as far down the column as data goes in Column E.
The reason I can't leave a static formula in the cell is because it relates to a chart and if there is a formula in the cells lower than data in column E it will extend the width of the graph.
As I am new to VBA in Excel I'm not sure how to do this. Can you help?
Thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hello Peter,

I'm assuming that you have a formula in F1 so perhaps the following may help:-

VBA Code:
Sub Test()

Dim lr As Long: lr = Sheet1.Range("E" & Rows.Count).End(xlUp).Row

Sheet1.Range("F1:F" & lr) = Range("F1").Formula

End Sub

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
I'll take a look as soon as I have finished the current work.
Thanks for your help.
I'll get back to you after I have tried it.
Cheers
 
Upvote 0
I've just tried it on a sample worksheet and it works great.
Really appreciate your help.

Cheers
Peter
 
Upvote 0
Sorry to bother you again.
I've just tried to put the formula in my large excel file and placed it in the middle of a larger macro and it isn't copying down the formula. I've had to change the sheet name and cell name from the above to match the sheet and columns I need it in.

This is the formula I am using -

Workbooks("filename.xlsm").Worksheets("rep writer variance report").Activate (This is to make the workbook and sheet active)
Dim lr As Long: lr = Range("F" & Rows.Count).End(xlUp).Row (This is your code above with the columns changed as per picture)

Range("G2:G" & lr) = Range("G2").Formula (This is your code above with the columns changed as per picture)

F is the column with the list of data and G2 is the cell with the formula in. I need it to copy G2 into G3 onwards until the row that F finishes the data. (Hopefully explained that ok.
I have a picture of the data below

Any further help would be great.

Data Cels.JPG
 
Upvote 0
How about
VBA Code:
 Range("G2:G" & lr).filldown
 
Upvote 0
No it should be as I showed. ;)
 
Upvote 0
The strange thing is the original formula you gave me works fine if I create a simple example on a sheet like below

See below
Data Cells.JPG
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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