VBA code to autofill based on a date?

three482

New Member
Joined
Jul 9, 2024
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi all.

I’m trying to add some code that will autofill cells based on the date in another cell.

The macro is currently set to newest file of the same name when ran. I have code that creates a new “Comments” column at the end (column G), but I want it to reference the Date column (column B) and autofill “Ignore” into column G if the date on that row is before 1/1/2020. Of the data is after 1/1/2020, leave blank.

For example, if the date in B1 is 12/1/2019, G1 would autofill “Ignore.” If the date in B2 is 2/1/2020, G2 would remain blank.

Thanks in advance!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Why do you need VBA code? Wouldn't a simple IF formula suffice, i.e.
Excel Formula:
=IF(B1<DATE(2020,1,1),"Ignore","")
and copy down for all rows.
 
Upvote 0
Why do you need VBA code? Wouldn't a simple IF formula suffice, i.e.
Excel Formula:
=IF(B1<DATE(2020,1,1),"Ignore","")
and copy down for all rows.
Yeah, I’ve tried a formula very similar to that one, but it’s a new file that imported daily so the formula would need to be added every day since I have no way of adding it to the source file. Hence, I wanted to add it to the macro to include as it runs.
 
Upvote 0
Here is VBA code that will populate column G with the formula you need in all row, down to the last row in column B with data.
VBA Code:
'   Find last row in column B with data
    Dim lr As Long
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Populate column G with formula
    Range("G1:G" & lr).FormulaR1C1 = "=IF(RC[-5]<DATE(2020,1,1),""Ignore"","""")"
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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