VBA AutoFill Varying Rows of Data

DBCox

Board Regular
Joined
Jul 5, 2012
Messages
52
Hello everyone,

I have an Excel workbook that has multiple sources of data that vary depending on the day. I am running into a problem with the equations refering the data giving me a #REF error when the number of data rows decreases. This is fairly easily corrected manually by selecting the top row of columns that contain equations and "dragging" them down to correct all of the references. The problem is, I need to do this with a macro. The following works for 16 rows of data, but obviously it will not work for varying rows of data. Furthermore, I am working with anything from 15,000 to 25,000 rows of data.

Code:
    Range("D1:E1").Select    Selection.AutoFill Destination:=Range("D1:E16"), Type:=xlFillDefault    Range("D1:E16").Select    Range("H12").Select


One solution is changing the range to "D1:E25000". The only issue with this is it will often AutoFill more equations than necessary and has potential to cause errors due to lack of data.Ideally, I could change the number preceding the "E" depending on the number of rows of data. I can easily get that number with a CountA function, but cannot seem to get the

Code:
Selection.AutoFill Destination:=Range("D1:E16"), Type:=xlFillDefault

line to work with a variable in the parenthesis.How can I easily vary the number of rows? Also, is there a way to directly pull the CountA value with VBA, rather than putting the equation in a cell and having VB pull the cell's value?

Thanks!

David
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Code:
LR=cells(rows.count,"A").end(xlup).row
Range("D1:E1").AutoFill Destination:=Range("D1:E" & LR), Type:=xlFillDefault
In the first line, substitute for "A" the column letter which is already full down to to.where yonpu want the formulawe autofilled do
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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