My VBA for formulas won't work if the data only has one row of info

butters149

New Member
Joined
Mar 21, 2018
Messages
23
Hello,

I created this code to apply this formula to the first row and then copy it down until the end, but then it does not work if the data/table only has one row. My code is below if anyone can help that would be great, I think it may just be something I am forgetting. thanks!

'Days Aged
Columns("I:I").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("I1").Select
ActiveCell.FormulaR1C1 = "Days Aged"
Range("I2").Select
ActiveCell.FormulaR1C1 = "=TODAY()-RC[-4]"
Range("I2", "I" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
Columns("I:I").Select
Selection.NumberFormat = "General"
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Maybe...

Code:
Sub xxxx()
    'Days Aged
    Columns("I:I").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("I1").Value = "Days Aged"
    If Cells(Rows.Count, 1).End(xlUp).Row > 1 Then _
       Range("I2", "I" & Cells(Rows.Count, 1).End(xlUp).Row).FormulaR1C1 = "=TODAY()-RC[-4]"
    Columns("I:I").NumberFormat = "General"
End Sub

Not sure why you want the column formatted as General rather than "mm/dd/yyyy" or "dd/mm/yyyy" depending on region though.
 
Upvote 0
Or depending on what you want to happen if it is one row maybe...
Code:
Sub xxxx()
    'Days Aged
    If Cells(Rows.Count, 1).End(xlUp).Row > 1 Then
        Columns("I:I").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("I1").Value = "Days Aged"
        Range("I2", "I" & Cells(Rows.Count, 1).End(xlUp).Row).FormulaR1C1 = "=TODAY()-RC[-4]"
        Columns("I:I").NumberFormat = "General"
    End If
End Sub

Still the below applies...
Not sure why you want the column formatted as General rather than "mm/dd/yyyy" or "dd/mm/yyyy" depending on region though.
 
Upvote 0
Thank-you! This worked.

Or depending on what you want to happen if it is one row maybe...
Code:
Sub xxxx()
    'Days Aged
    If Cells(Rows.Count, 1).End(xlUp).Row > 1 Then
        Columns("I:I").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("I1").Value = "Days Aged"
        Range("I2", "I" & Cells(Rows.Count, 1).End(xlUp).Row).FormulaR1C1 = "=TODAY()-RC[-4]"
        Columns("I:I").NumberFormat = "General"
    End If
End Sub

Still the below applies...
Not sure why you want the column formatted as General rather than "mm/dd/yyyy" or "dd/mm/yyyy" depending on region though.
 
Upvote 0

Forum statistics

Threads
1,223,969
Messages
6,175,680
Members
452,667
Latest member
vanessavalentino83

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