Macro to autofill formula down many ranges

bdd12

New Member
Joined
May 24, 2022
Messages
10
Office Version
  1. 365
Platform
  1. MacOS
I am trying to create a macro that will autofill three columns worth of formulas until the end of that range and then repeat those formulas for each range below it. For context, the file is a compilation of thousands of different series that could have as little as one line, or as many as dozens. Each series is split up by one blank row, so if the first range is from A1:U7, the next range would start in A9 and go to U(x), with x being whatever row the next block ends on, an so on. I have copied and pasted the code I have written so far, just need to find a way to autofill these formulas just until the end of the current range, and then loop that so it will run the same formulas for all the ranges below that are separated by one blank row

ActiveCell.FormulaR1C1 = "=DATE(YEAR(RC[-22]),MONTH(RC[-22]),DAY(RC[-22]))"
Range("AB2").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISBLANK(RC[-18])=FALSE,IF(RC[-1]<RC[-18],RC[-1],""DONE""),RC[-1])"
Range("AC2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=RC[-8],RC[-1],""ERROR"")"
Range("AA3").Select
ActiveCell.FormulaR1C1 = _
"=DATE(YEAR(R[-1]C),MONTH(R[-1]C)+R[-1]C[-20],DAY(R[-1]C))"
Range("AB3").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISBLANK(R[-1]C[-18])=FALSE,IF(R[-1]C[-1]<R[-1]C[-18],R[-1]C[-1],""DONE""),R[-1]C[-1])"
Range("AC3").Select
ActiveCell.FormulaR1C1 = "=IF(R[-1]C[-1]=R[-1]C[-8],R[-1]C[-1],""ERROR"")"
Columns("AA:AC").Select
Selection.NumberFormat = "m/d/yy"
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Welcome to the Board!

In determining which rows to apply this to, I assume that you have at least one column that will ALWAYS have data in it for any row with data. Which column is that, exactly?
And which columns exactly are you populating (in your first line, ActiveCell does not tell us where you are starting from)?
 
Upvote 0
Thank you! Yes, there are multiple columns that always have data when they aren't the blank row, one of them is column G. The first cell being populated is AA2, it goes across to AC2, and will need to go down as far as the last block of cells with data in it, which will change every time the report is pulled
 
Upvote 0
The cool thing is that you usually do not need to select a range in order to work with it (and it is usually faster and more efficent not to).
See if this does what you want:
VBA Code:
Sub MyMacro()

    Dim lr As Long
    Dim rng As Range
    
'   Find last row in column G with data
    lr = Cells(Rows.Count, "G").End(xlUp).Row
    
'   Set range equal to all non-blank cells in column G
    Set rng = Range("G2:G" & lr).SpecialCells(xlCellTypeConstants, 23)
    
'   Set formula for column AA
    rng.Offset(0, 20).FormulaR1C1 = "=DATE(YEAR(RC[-22]),MONTH(RC[-22]),DAY(RC[-22]))"
    
'   Set formula for column AB
    rng.Offset(0, 21).FormulaR1C1 = "=IF(ISBLANK(RC[-18])=FALSE,IF(RC[-1]<RC[-18],RC[-1],""DONE""),RC[-1])"
    
'   Set formula for column AC
    rng.Offset(0, 22).FormulaR1C1 = "=IF(R[-1]C[-1]=R[-1]C[-8],R[-1]C[-1],""ERROR"")"
    
'   Set number format
    Columns("AA:AC").NumberFormat = "m/d/yy"
    
End Sub
 
Upvote 0
The cool thing is that you usually do not need to select a range in order to work with it (and it is usually faster and more efficent not to).
See if this does what you want:
VBA Code:
Sub MyMacro()

    Dim lr As Long
    Dim rng As Range
   
'   Find last row in column G with data
    lr = Cells(Rows.Count, "G").End(xlUp).Row
   
'   Set range equal to all non-blank cells in column G
    Set rng = Range("G2:G" & lr).SpecialCells(xlCellTypeConstants, 23)
   
'   Set formula for column AA
    rng.Offset(0, 20).FormulaR1C1 = "=DATE(YEAR(RC[-22]),MONTH(RC[-22]),DAY(RC[-22]))"
   
'   Set formula for column AB
    rng.Offset(0, 21).FormulaR1C1 = "=IF(ISBLANK(RC[-18])=FALSE,IF(RC[-1]<RC[-18],RC[-1],""DONE""),RC[-1])"
   
'   Set formula for column AC
    rng.Offset(0, 22).FormulaR1C1 = "=IF(R[-1]C[-1]=R[-1]C[-8],R[-1]C[-1],""ERROR"")"
   
'   Set number format
    Columns("AA:AC").NumberFormat = "m/d/yy"
   
End Sub
Awesome! That worked in that it was able to create formulas in the correct cells for the entire worksheet, there is just a slight problem in that in column AA there are actually two different formulas. With how the export is set up the first line in each block needs to reference the corresponding cell in column E, but all lines below need to reference the cell directly above them, see the difference between:

ActiveCell.FormulaR1C1 = "=DATE(YEAR(RC[-22]),MONTH(RC[-22]),DAY(RC[-22]))" (which is for the first line in each data block in column AA) and:
ActiveCell.FormulaR1C1 = "=DATE(YEAR(R[-1]C),MONTH(R[-1]C)+R[-1]C[-20],DAY(R[-1]C))" (which is for all lines below it)

Is there a way to do that?
 
Upvote 0
I think you need to show us what this data structure looks like, so it is clear to us what formulas need to go where.
It is hard for us to determine when we really have no idea what it all looks like.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
grantSeriesReport.2022-04-18.csv
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1AmountStart DateRecurrence RuleFrequencyUnitUpdated FrequencyEnd DateTotal TimesPayment TypeTotal Expected InstancesTotal InstancesDifferenceActionValueNoteExpected Instance DateExisting Instance Scheduled DateSame Date?Existing Instance AmountExisting Instance Status
2-1003/1/22every 3 months for 6 times3monthly361103/1/223/1/22TRUE-100COMPLETE
3
4-19006/17/21every month1monthly1121116/17/216/17/21TRUE-1900COMPLETE
5-19006/17/21every month1monthly1121117/17/217/17/21TRUE-1900COMPLETE
6-19006/17/21every month1monthly1121118/17/218/17/21TRUE-1900COMPLETE
7-19006/17/21every month1monthly1121119/17/219/17/21TRUE-1900COMPLETE
8-19006/17/21every month1monthly11211110/17/2110/17/21TRUE-1900COMPLETE
9-19006/17/21every month1monthly11211111/17/2111/17/21TRUE-1900COMPLETE
10-19006/17/21every month1monthly11211112/17/2112/17/21TRUE-1900COMPLETE
11-19006/17/21every month1monthly1121111/17/221/17/22TRUE-1900COMPLETE
12-19006/17/21every month1monthly1121112/17/222/17/22TRUE-1900COMPLETE
13-19006/17/21every month1monthly1121113/17/223/17/22TRUE-1900COMPLETE
14-19006/17/21every month1monthly1121114/17/224/17/22TRUE-1900APPROVED
15-19006/17/21every month1monthly1121115/17/22
16
17-5005/2/22every month1monthly11105/2/225/2/22TRUE-500SUBMITTED
18
19-4007/1/21every month1monthly11112-17/1/216/7/21FALSE-400COMPLETE
20-4007/1/21every month1monthly11112-18/1/217/1/21FALSE-400COMPLETE
21-4007/1/21every month1monthly11112-19/1/218/1/21FALSE-400COMPLETE
22-4007/1/21every month1monthly11112-110/1/219/1/21FALSE-400COMPLETE
23-4007/1/21every month1monthly11112-111/1/2110/1/21FALSE-400COMPLETE
24-4007/1/21every month1monthly11112-112/1/2111/1/21FALSE-400COMPLETE
25-4007/1/21every month1monthly11112-11/1/2212/1/21FALSE-400COMPLETE
26-4007/1/21every month1monthly11112-12/1/221/1/22FALSE-400COMPLETE
27-4007/1/21every month1monthly11112-13/1/222/1/22FALSE-400COMPLETE
28-4007/1/21every month1monthly11112-14/1/223/1/22FALSE-400COMPLETE
29-4007/1/21every month1monthly11112-15/1/224/1/22FALSE-400PAID
30-4007/1/21every month1monthly11112-15/1/22-400SUBMITTED
31
32-3001/16/22every month until March 6, 20221monthly13/6/2224-21/16/221/16/22TRUE-580COMPLETE
grantSeriesReport.2022-04-18
 
Upvote 0
Hopefully that worked, I deleted some columns of irrelevant data that is specific to my company and left the ones I need for the formula. As you can see in column E, that just shows the start date and so in order to go one month ahead in the formulas I created, I can't reference that column every time. If there is a better way of setting up the formula I am open to it, but how I currently have it the first row needs to reference column E, but all others need to reference the cell directly above them
 
Upvote 0
I think rather than trying to figure out two different formulas and when to use which, it is better to create one long formula with an IF statement, to determine when to use each one.
Try this:
VBA Code:
Sub MyMacro()

    Dim lr As Long
    Dim rng As Range
    
'   Find last row in column G with data
    lr = Cells(Rows.Count, "G").End(xlUp).Row
    
'   Set range equal to all non-blank cells in column G
    Set rng = Range("G2:G" & lr).SpecialCells(xlCellTypeConstants, 23)
    
'   Set formula for column AA
    rng.Offset(0, 20).FormulaR1C1 = _
        "=IF(ISNUMBER(R[-1]C[-22]),DATE(YEAR(R[-1]C),MONTH(R[-1]C)+R[-1]C[-20],DAY(R[-1]C)),DATE(YEAR(RC[-22]),MONTH(RC[-22]),DAY(RC[-22])))"
    
'   Set formula for column AB
    rng.Offset(0, 21).FormulaR1C1 = "=IF(ISBLANK(RC[-18])=FALSE,IF(RC[-1]<RC[-18],RC[-1],""DONE""),RC[-1])"
    
'   Set formula for column AC
    rng.Offset(0, 22).FormulaR1C1 = "=IF(R[-1]C[-1]=R[-1]C[-8],R[-1]C[-1],""ERROR"")"
    
'   Set number format
    Columns("AA:AC").NumberFormat = "m/d/yy"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,149
Messages
6,123,311
Members
449,095
Latest member
Chestertim

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