VBA Function which is somewhat a Reverse of CHOOSE

x0nar

New Member
Joined
May 10, 2016
Messages
34
We all know how CHOOSE Works

eg. If I had month number and I wanted to convert them into Month name then I'd be using Choose
ie.
Code:
MonName=CHOOSE(Monno,"Jan", "Feb", "Mar", "Apr", "May" "Jun", "Jul", "Aug", "Sep", "Oct" "Nov" "Dec")
But what my query is that if we were given month names and were asked to find their respective numbers how would one do that.

I am aware of the use of
Code:
InStr
to find the relative number of a character from a string.

What I wanted to ask is if it was possible to consider a word instead of a single character to find out their relative number from a string.

Thanks.
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi, if your string that contains your month name is, for example:

Code:
Dim sMonth As String
sMonth = "May"

Then for your specific scenario you could use something like this to get the month number:
Code:
MsgBox Month(sMonth & "-1")

Or more generally you could use Excels Match() function:
Code:
MsgBox Application.Match(sMonth, Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), 0)

And FWIW, you can also get the month name by using the MonthNane function:
Code:
Dim Mono As Long
Mono = 5
MsgBox MonthName(Mono)
 
Last edited:
Upvote 0
You can use simple formula :

=MONTH(A1&1)

or

=MONTH(DATEVALUE(A1&" 1"))

or

=MONTH(DATEVALUE("01-"&A1))

or

=MATCH(A1,{"January","February","March","April","May","June","July","August","September","October","November","December"},0)
 
Upvote 0
Hi, if your string that contains your month name is, for example:

Code:
Dim sMonth As String
sMonth = "May"

Then for your specific scenario you could use something like this to get the month number:
Code:
MsgBox Month(sMonth & "-1")

Or more generally you could use Excels Match() function:
Code:
MsgBox Application.Match(sMonth, Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), 0)

And FWIW, you can also get the month name by using the MonthNane function:
Code:
Dim Mono As Long
Mono = 5
MsgBox MonthName(Mono)

Thanks MATCH was exactly what I was looking for.
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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