Developer

Use a VBA function to populate list controls with names of months

You can quickly populate Office list controls with the months of the year. Thanks to the MonthName() function, a simple VBA loop will do the trick.

Populating a combo or list box with the months of the year isn't difficult. You can type the literal names as the control's row source or use the MonthName() function in a simple VBA For loop.

When displaying months in a control on a userform (Excel and Word), use the following code to populate a list control named cboMonths with the names of the months:

Private Sub UserForm_Activate()

'Populate cboMonths with months, by name.

Dim i As Integer

For i = 1 To 12

cboMonths.AddItem MonthName(i)

Next

End Sub

You can use similar code to populate a list control in Access, but use the form's Load or Open event as follows:

Private Sub Form_Load()

'Populate cboMonths with months, by name.

Dim i As Integer

For i = 1 To 12

cboMonths.AddItem MonthName(i)

Next

End Sub

Be sure to set the list control's Row Source Type to Value List (in Access).

About Susan Harkins

Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she was editor in chief for The Cobb Group, the world's largest publisher of technical journals.

Editor's Picks

Free Newsletters, In your Inbox