Update the Date

G-fer

Board Regular
Joined
Jul 18, 2005
Messages
192
Hi all ..

I can't figure out how to update a date variable.

Column A is a date column
A1 has a date in it
A2 to A4 are empty
A5 has another date in it.

In my code, the date variable, ThisDate, is assigned the value of A1

The code eventually loops down to the next A cell.

If this is empty, I don't want to change ThisDate. However, if the cell contains a date, I want to change the value of ThisDate to the new date.

But I keep getting 12:00:00 AM as the date in the empty cells ?????

Graham
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I think it would be useful if you posted the relevant part of your code so we can see exactly what you are attempting to do.
 
Upvote 0
Here goes .....

Sub updatedate()

Dim ThisDate As Date
Dim NextCell As Integer

Range("A1").Select
ThisDate = Selection.Value

For NextCell = 1 To 4

Selection.Offset(1, 0).Select
If Not IsEmpty(ActiveCell.Value) Or IsNull(ActiveCell.Value) Then ThisDate = Selection.Value
MsgBox (ThisDate)

Next

End Sub


On the spreadsheet, A1 has a date, A2 is empty. However, the message shows the content of A2 to be 12:00:00 AM

G.

Oh Oh ...

This seems to work as a stand alone routine. I guess I have an error somewhere in the rest of the code. Sorry about taking up your time folks.

G
 
Last edited:
Upvote 0
The reason the MsgBox is showing 12:00:00 AM is because the you Dim'med the ThisDate variable as a Date... Date variables cannot be empty. In VB, dates are stored as floating point numbers... the whole number part represents the number of days offset from "date zero" (December 30, 1899) and the decimal part represents the time expressed as a fractional part of a 24-hour day. When nothing is assigned to a Date variable, it is automatically 0. As a convenience for being able to work with times only, VB suppresses the 12/30/1899 display for "date zero" and just displays the time value... a 0 time value is midnight (12:00:00 AM).
 
Upvote 0

Forum statistics

Threads
1,224,547
Messages
6,179,436
Members
452,915
Latest member
hannnahheileen

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