ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Defining a Variable (https://www.excelbanter.com/excel-programming/276421-defining-variable.html)

Stephen[_3_]

Defining a Variable
 
Hi -

I'm trying to define a variable based on the contents in a cell. I
used the variable in different times in the macro to define the month
of the year. Here is what I came up with so far.

The data I receive has the date in the following format in cell A1.
8/20/2003 12:23 PM

In my macro, I have a variable defined as follows.
month = left(A1,1)
Result - The month variable in the example above would be 8. This is
what I am looking for.

The problem I have now is that when October comes around, the month
variable will only pull the one number (1 for 10). If I change the
month variable to (month = left(A1,2)), I would get 8/ as my variable
in the example above.

Is there a way to add an if statement in defining a variable.

Thanks.
Stephen

Bob Phillips[_5_]

Defining a Variable
 
Of course, in VBA it would be

myMonth=MONTH(Range("A1").Value)

--

HTH

Bob Phillips

"Bob Phillips" wrote in message
...
Stephen,

Why not use
month = MONTH(A1)

probably best to rename the variable le as well, not use a VBA keyword.

--

HTH

Bob Phillips

"Stephen" wrote in message
m...
Hi -

I'm trying to define a variable based on the contents in a cell. I
used the variable in different times in the macro to define the month
of the year. Here is what I came up with so far.

The data I receive has the date in the following format in cell A1.
8/20/2003 12:23 PM

In my macro, I have a variable defined as follows.
month = left(A1,1)
Result - The month variable in the example above would be 8. This is
what I am looking for.

The problem I have now is that when October comes around, the month
variable will only pull the one number (1 for 10). If I change the
month variable to (month = left(A1,2)), I would get 8/ as my variable
in the example above.

Is there a way to add an if statement in defining a variable.

Thanks.
Stephen







All times are GMT +1. The time now is 08:46 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com