How to Convert a Month Name to a Number in Excel VBA
Question details
The user needs to convert a month name selected in an Excel VBA ListBox into a numeric month value.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Working with a UserForm ListBox in Excel VBA containing a chronological list of month names.
- Observed behavior
- The goal is to programmatically extract the user's selected month string from the ListBox and translate it into a 1-12 integer representation.
Ensure your ListBox is populated with month names in strict chronological order (January to December) and verify that macros are enabled in your workbook settings.
Use the ListBox ListIndex Property
The most efficient way to get a numeric month from a chronologically ordered ListBox is by leveraging its zero-based index.
In VBA, a ListBox's ListIndex property returns the position of the currently selected item. Since the index is zero-based (January = 0, February = 1, etc.), adding 1 to this property directly gives you the correct calendar month number.
Make sure your ListBox control (e.g., ListBox1) is populated exactly from January to December.
Add an If statement to ensure an item is actually selected before calculating. Use `If ListBox1.ListIndex <> -1 Then` to verify a selection exists.
Inside your UserForm code module, use the expression `MonthNum = ListBox1.ListIndex + 1` to assign the numeric value to your variable.

Use the Month and DateValue Functions
If your ListBox items are not in chronological order or you are extracting the string value directly, you can convert the string name to a date.
Easily Execute VBA Code and UserForms with WPS Office
WPS Office provides excellent compatibility with Excel VBA, allowing you to run your macros, ListBox controls, and UserForms seamlessly without rewriting your code.
- 1. Download and Install: Download WPS Office Free and install it on your computer.
- 2. Open your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the ListBox UserForm.
- 3. Enable Macros: Click 'Enable Macros' on the security warning bar at the top of the worksheet to allow VBA execution.
- 4. Access the VBA Editor: Go to the 'Developer' tab and click 'Visual Basic' to view, edit, or run your ListBox code.

Frequently Asked Questions
What happens if no month is selected in the ListBox?
If no item is selected, the ListIndex property returns -1. Adding 1 to this would result in 0, which is an invalid calendar month. You should always check `If ListBox1.ListIndex <> -1` before executing your conversion code.
Does this method work if my ListBox starts with a month other than January?
No, the `ListIndex + 1` method strictly relies on January being the first item (Index 0). If your list starts with another month (like a fiscal year starting in April), you must either calculate the offset mathematically or use string-to-date conversion functions like `Month(DateValue(monthName & " 1"))`.
Can I use the selected text value instead of the ListIndex?
Yes, you can retrieve the actual month string using `ListBox1.Value` or `ListBox1.Text`, and then convert it using VBA's `Month` and `DateValue` functions. However, if your list is perfectly ordered from January to December, using `ListIndex` is generally simpler and executes faster.




