Subtract dates VBA

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I'm using a formula to subtract dates from today. I would like to use a vba to go through each row (I3:I12) to calculate the days remaining from the dates in column (H3:H12). Greeatly appreciate any assistance. Thank you kindly
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Why use VBA when you can use formula? if it is days then just Date2-Date1 or Date2-(Date1-1) depending whether you include the start day or not.
Btw the syntax if you are using DATEDIF (DATEDIF not DATEDIFF) in formula is DATEDIF(date1,date2,"d")


Excel 2010
ABC
101/10/201819
220/10/201819
320
Sheet1
Cell Formulas
RangeFormula
C1=DATEDIF(A1,A2,"d")
C2=A2-A1
C3=A2-(A1-1)


Btw, you can just subtract one date from the other in VBA as well if it is days rather than use DATEDIFF. No need for the function call.
 
Last edited:
Upvote 0
DATEDIF is the spreadsheet function. DATEDIFF is the VBA function.
 
Upvote 0
I search and found a code similar to what I want to achieve (see below). I tried tiredlessly to alter it to my needs, but i'm at a loss. I would like the code to only be used from Row 3-12. Aside from that, the code achieves the task that I'm looking for. In case any inquiries as to what I will be using the code for is to run it when the sheet is activated. Can someone assist with my request. Thanks.


Code:
Sub Test1()Dim LastRow  As Long, i As Long
With Worksheets("Options")
    LastRow = .Cells(.Rows.Count, "H").End(xlUp).Row
    
    For i = 3 To LastRow
        .Range("I" & i).Value = DateDiff("d", .Range("H" & i).Value, Date)
    Next i
End With
End Sub
 
Upvote 0
One other thing, sometimes a row will be empty, so I don't want the calculation to occurs if a cell in the Range H3:H12 is empty.
 
Upvote 0
Code:
Sub Test1a()
    Dim i As Long
    With Worksheets("Options")
        For i = 3 To 12
            If .Range("H" & i).Value <> "" Then
                .Range("I" & i).Value = Date - .Range("H" & i).Value
            End If
        Next i
    End With
End Sub
or
Code:
Sub Test1b()
    Dim i As Long
    With Worksheets("Options")
        For i = 3 To 12
            If .Range("H" & i).Value <> "" Then
                .Range("I" & i).Value = DateDiff("d", .Range("H" & i).Value, Date)
            End If
        Next i
    End With
End Sub

The code above runs from a regular module but if by
run it when the sheet is activated
you mean you want the code to run automatically then it needs a total rewrite.
 
Last edited:
Upvote 0
Works perfect Mark! Yes, I will be placing the code in the Worksheet Activate to run when the sheet is activated. Thank you very much everyone. Sincerely appreciated it. Out of curiosity, what is the difference between the two codes?

Code:
Sub Test1a()
    Dim i As Long
    With Worksheets("Options")
        For i = 3 To 12
            If .Range("H" & i).Value <> "" Then
                .Range("I" & i).Value = Date - .Range("H" & i).Value
            End If
        Next i
    End With
End Sub
or
Code:
Sub Test1b()
    Dim i As Long
    With Worksheets("Options")
        For i = 3 To 12
            If .Range("H" & i).Value <> "" Then
                .Range("I" & i).Value = DateDiff("d", .Range("H" & i).Value, Date)
            End If
        Next i
    End With
End Sub

The code above runs from a regular module but if by you mean you want the code to run automatically then it needs a total rewrite.
 
Upvote 0
Disregard..I see the difference. Thank you very much Mark
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,608
Members
449,038
Latest member
apwr

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