VBA How many days has passed since date from cell

LBee

New Member
Joined
Dec 25, 2021
Messages
20
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have this Macro, which look in column A where I have different dates, and writes in column B how many days has passed since the date in column A

VBA Code:
Sub Test()

    Dim Lastrow As Long
    Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
   

    Range("B2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-1]>TODAY()+TIME(0,0,0),0,ROUNDUP(TODAY()-RC[-1],0))"
    Range("B2").Select
    Selection.AutoFill Destination:=Range("B2:" & "B" & Lastrow)
    Range("B2:" & "B" & Lastrow).Select
    Selection.NumberFormat = "@"

   
End Sub

The above code works fine, but I'm trying to replace TODAY with a fixed date I have in cell D2, and it looks easy, but I really can't figure it out:cry:
I have tried so many different things, but everything gives me an error, or a strange date instead of number of days.
Can someone give me a little push in the right direction?
 
Last edited by a moderator:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
try replacing TODAY() with R2C4.

By the way, you don't need all the .Select's, e.g.

VBA Code:
'instead of
Range("B2").Select
ActiveCell.FormulaR1C1 = _

'you can have
Range("B2").FormulaR1C1 = _

Another way you could do it:

Code:
With Range("B2:" & "B" & Lastrow)
    .Formula = "=MAX(0,D$2-INT(A2))"
    .NumberFormat = "@"
End With
 
Upvote 0
Solution
Hi StephenCrump

Thanks a lot for your reply, it solved my problem.
The reason why I wanted a cell name instead of the R1C1 formula, is because its a part of a bigger macro, and I reuse the code in the different step (just took this part out for simplicity), but this could actually work.

I'm quite new to this, and have never used the With function, but it look a lot simpler than my code, and I definitely have to look in to that.

Once again - Thanks a lot ?
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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