Using TEXT(A1,"mmyy") in VBA?

Drofidnas

Board Regular
Joined
Jun 30, 2004
Messages
126
Hi

By using

Code:
=IF(TEXT($I18,"mmyy")=TEXT(L$2,"mmyy"),1,0)

I can perform a check of the two cells and even though one is formatted as Oct-14 an the other is 23/10/14

I will get a 1 - ie, they are in the same month.

How can I do this programmatically with VBA?

I have

Code:
If Cells(18,9) = Cells (2,12) then
Cells (4,2) = 1 
End If

but I need some formatting around those statements.

Thanks for any help

Chris
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
If Format(Range("I18"),"mmyy") = Format(Range("L2"),"mmyy") Then
'Do something
End If
 
Upvote 0
You could try:
Code:
If [COLOR=#ff0000][B]Month([/B][/COLOR]Cells(18,9)[COLOR=#ff0000][B])[/B][/COLOR] = [COLOR=#ff0000][B]Month([/B][/COLOR]Cells (2,12)[COLOR=#ff0000][B])[/B][/COLOR] then 
      Cells (4,2) = 1 
End If
 
Upvote 0
Hi

By using

Code:
=IF(TEXT($I18,"mmyy")=TEXT(L$2,"mmyy"),1,0)

I can perform a check of the two cells and even though one is formatted as Oct-14 an the other is 23/10/14
If you wanted to do it structured...
Code:
If Format(Range("I18").Value, "mmyy") = Format(Range("L2").Value, "mmyy") Then
  ' Month and year are the same
Else
  ' Month or year is different
End If
otherwise if you wanted to assign the condition to a variable, declare the variable (example name Result) as Boolean and do this...
Code:
Result = Format(Range("I18").Value, "mmyy") = Format(Range("L2").Value, "mmyy")
 
Upvote 0
In VBA you can use the Format ( expression, [ format ] ) to much the same effect.

For more information, I personally look at:
MS Excel: FORMAT Function with Strings (VBA)

For your purposes I suspect you want something like

If Format(Range, Short Date) = Format(Other Range, Short Date) Then
*CODE HERE
End If

 
Upvote 0
For your purposes I suspect you want something like

If Format(Range, Short Date) = Format(Other Range, Short Date) Then
*CODE HERE
End If

Given the OP's example formula, your suggestion would not work if the days were different.
 
Upvote 0
Thanks Joe and Rick. Exactly what I wanted. I was playing with Format but couldn't get the syntax right!

Many thanks indeed!
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top