Discussion Locked

Vba DateSerial function

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I am currently having an issue using the DateSerial function in vba. When a date with the 31st day of a month is input as a variant/string type a Type Mismatch error occurs. This only happens with the 31st day of the month. All other dates work.Is there another way to convert a variant/string to a variant/date?

5 replies

by sl-campbell

1. The DateSerial accepts the input of Year,Month,Date as in DateValue(2002,2,20)

2. You can change a string to a date using the CDate function: CDate(myDate) where myDate="02/20/2002"

2. You can also ?se the DateValue function: DateValue(myDate)

by hmertens

Try reproducing the error under OL2000/VBA in debug mode (immediate window) mode and couldn't, e.g.
?vba.DateSerial("2002","02","31")
and
x="2002"
y="2"
z="31"
?vba.DateSerial(x,y,z)

What version of VBA are you using (I using 6.0.8714with OL2K)?

As a coding solution using my second approach above, you could try this:

x="2002"
y="2"
z="31"
?vba.DateSerial(vba.CInt(x),vba.CInt(y),vba.CInt(z))

NB: This should return a date of 03/03/2002 (March 3rd...)

This discussion is closed to new replies.