Calculate difference between two dates in cells by using activecell.offset and datediff

Prevost

Board Regular
Joined
Jan 23, 2014
Messages
198
Hi There,

I need to calculate the difference between two dates (in days) using VBA. I want the date to be put into the active cell and the start date and end date are referenced from that active cell. I think that my variables are not declared correctly or I need to convert the activecell.offset values into some variable so that VBA knows what to do with it. There was actually the same post (unfortunately with no answer) at []vba - Excel 2010 - Using ActiveCell.Offset to set variables for use in DateDiff macro - Stack Overflow so I thought I would try my luck with MrExcel. My date cells are in the form mm/dd/yyyy. Thanks in advance.
Windows 7 & Excel 2007

Code:
<code>Dim date1 As Date Dim date2 As Date date1 = ActiveCell.Offset(-3, 0).Value date2 = ActiveCell.Offset(-1, 0).Value ActiveCell.Value = DATEDIFF("d", date1, date2)</code></pre>
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
In case anyone is wondering, this code does work. However, I had to swap around the row and column offset for some reason. I will start a new thread to see if anyone knows why.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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