Now Function Question

N

Nausett

I'm trying to use the Now function in VBA. I continue to get a compile
error: Expected function or variable. The error highlights on the word, Now.
The formula below is an example of the code.

Sub Dateparttest()
Dim today
today = Month(Now)
Range("b1").Value = today

End Sub
 
M

Mark Lincoln

To put the current date into B1, just do this:

Range("B1").Value = Date

In VBA, Date returns the current system date.

Mark Lincoln
 
R

Rick Rothstein \(MVP - VB\)

I'm trying to use the Now function in VBA. I continue to get a compile
error: Expected function or variable. The error highlights on the word,
Now.
The formula below is an example of the code.

Sub Dateparttest()
Dim today
today = Month(Now)
Range("b1").Value = today
End Sub

Although I am not sure why VBA cares about it, the variable name "today" is
where your problem is. I'm guessing it is a reserved keyword for VBA inside
of Excel... change the variable name and your code should work fine (unless
you stumble upon another keyword, that is<g>).

Rick
 
M

Michael

Try this:
Sub Dateparttest()
Today = Now
Range("B1").Value = Today
Range("B1").NumberFormat = "mmmm"

End Sub
 
M

Mark Lincoln

Although I am not sure why VBA cares about it, the variable name "today" is
where your problem is. I'm guessing it is a reserved keyword for VBA inside
of Excel... change the variable name and your code should work fine (unless
you stumble upon another keyword, that is<g>).

Rick

Today is an Excel function.
 
M

Mark Lincoln

In my previous post, I missed that you are looking to put the month
into B1. If you set the formatting of B1 to "mmmm", it will display
the month.
 
R

Rick Rothstein \(MVP - VB\)

I'm trying to use the Now function in VBA. I continue to get a compile
Today is an Excel function.

I realize that at the spreadsheet level, but Today does not seem to be
"defined" within the VBA world itself.

Rick
 
M

Mark Lincoln

That's what I was trying to say. One uses Date in VBA, but Today in
an Excel workbook. And people wonder why I often look confused....

Mark Lincoln
 
V

Vijay Chary

Hi Nausett !! :)
Today is an Excel Function similar to Now. Try
using another variable name instead of Today !!
 

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

Top