PDA

View Full Version : Excel VBA recognizing the variable as Date wrong



crazyjji
08-10-2009, 08:01 PM
The following is the code that I used to use, which worked fine before I reinstall window and thus the excel.

Sub MM()
Dim UPdate As Date
Range("A4").Formula = "=TODAY()"
UPdate = ActiveCell.Value
Range("A4").Value = UPdate
End Sub


However, after reinstallation, rather than the cell A4 being today's date, it shows 12:00:00 AM. So I have to use the following code to prevent this keep coming back.

Sub MM()
Dim UPdate As String
Range("A4").Formula = "=TODAY()"
UPdate = ActiveCell.Value
Range("A4").Value = UPdate
End Sub


What is wrong with my excel???????
The funny thing is if I execute the first code one and erase the cell and re-execute the code again, it works fine. Weird
Anyone know?????

mikerickson
08-10-2009, 08:08 PM
Cells can only contain data types String, Double, Boolean and Error.
Try Dimming UPdate as Double

or skip the intermediate formula and use

Range("A4").Value = Date

crazyjji
08-10-2009, 08:32 PM
But, I have been using the first code to many other algorithms and it worked fine for long time....

mikerickson
08-11-2009, 07:08 AM
Apparently something went odd with the re-install of Windows.