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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,069
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
If Format(Range("I18"),"mmyy") = Format(Range("L2"),"mmyy") Then
'Do something
End If
 
Upvote 0

bbott

Well-known Member
Joined
Feb 5, 2010
Messages
2,350
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

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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

ChrisBM

Board Regular
Joined
Sep 22, 2014
Messages
215
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

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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

Drofidnas

Board Regular
Joined
Jun 30, 2004
Messages
126
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,191,534
Messages
5,987,142
Members
440,082
Latest member
belodelokelo

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
Top