Selecting 31 list boxes using loop on VBA

mane_uk

New Member
Joined
May 17, 2011
Messages
4
Hi all,

I am trying to do a "calendar" display type using list boxes for each day - lbxDay1, lbxDay2,..., lbxDay30, lbxDay31 - in an Excel file.

I am OK to grab the data which will populate the list boxes but I still can't find a way to specify which list box I want to include the data in without being specific on the name.

I was wondering if there is any VBA code that would loop through the list boxes or something like a For/Next which would count 1 to 31 and pick the list box. Something like the code below:
Code:
For i=1 to 31
   lbxDayi.addItem("bla bla bla")
Next

Many thanks for your help!!
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You can modify the following script to suite what you want to do.

Code:
'To clear or change various listtboxes or buttons, first change there tags to 'an x or to any character you decide, you will need to change the x in the 'script to match your value though.  
 
For Each Ctrl In Me.Controls
        If Ctrl.Tag = "x" Then
            Select Case TypeName(Ctrl)
 
' the following will clear each listbox which has a tag of "X"

                Case "ListBox": Ctrl.Value = ""
                
 
            End Select
        End If
    Next Ctrl
 
Upvote 0
Hi Jaye7,

Thanks for your reply.

Just a quick question, in the code you sent I would still have to specify the list box name in the Select Case, wouldn't I!?

Cheers
 
Upvote 0
You do not have to specify the listbox as it cycles through them and looks for the Tag value of x and then it will add what ever script you specify.

It is a great tool to update multiple listboxes or textboxes.
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,823
Members
452,946
Latest member
JoseDavid

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