using dateserial in vb

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

am trying to convert date info into it's serial date value with following
code (month and year provided by user):

acctmon = Application.InputBox("Please enter month value between 1 and 12",
"Enter month", Month(Now), , , , , 1)

acctyr = Application.InputBox("Please enter year", "Enter year", Year(Now),
, , , , 1)

Dim date As Integer
date = Application.DateSerial(acctyr, acctmon, 1)

however, get the error message: run-time error '438': object doesn't support
this property or method

what does this mean, and what do i have to do to return a serial value? (is
there another function i can use to accomplish this?)
 
First, I wouldn't use date for a variable name since it is a vba keyword.
Second, if you declare it as an integer, you can't use dates after 9/16/89
w/o getting an overflow error. Third, leave out application when you call
dateserial.

Try:

Dim lngDate as long
lngDate = DateSerial(acctyr, acctmon, 1)
 
dear JMB, thanks answering and also for the quick response, definitely works
now. did have question, could i have used functions like DATEVALUE or DATE
to obtain serial value, and if so, what would the code look like? (just want
the simple, basic stuff, thank you)
 
datevalue is for converting string representations of dates to serial date
values

MyDate = DateValue("February 12, 1969")
MyDate = DateValue("2/12/1969")

but from VBA help:
If date is a string that includes only numbers separated by valid date
separators, DateValue recognizes the order for month, day, and year according
to the Short Date format you specified for your system.

which could cause you a problem if the code is intended to run on different
machines as their settings could vary.

The Date function, in VBA, returns the current system date - it is not the
same as the XL worksheet function Date.

Since your data obtained from the user is numeric, I'd stick w/Dateserial.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top