USERFORMS: Populating multiple comboboxes with the same data

liampog

Active Member
Joined
Aug 3, 2010
Messages
316
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
The following code is located in the Userform_Initialise part. DayPB, MidPB and NightPB are all comboboxes that are required to display the same data from the spreadsheet.

Code:
With Me.DayPB
        For PBNames = 132 To 141
            If Range("B" & PBNames).Value <> "" Then
            .AddItem Range("B" & PBNames)
            Else: Exit For
            End If
        Next PBNames
        .ListIndex = 0
    End With
    
    With Me.MidPB
        For PBNames = 132 To 141
            If Range("B" & PBNames).Value <> "" Then
            .AddItem Range("B" & PBNames)
            Else: Exit For
            End If
        Next PBNames
        .ListIndex = 0
    End With
    
    With Me.NightPB
        For PBNames = 132 To 141
            If Range("B" & PBNames).Value <> "" Then
            .AddItem Range("B" & PBNames)
            Else: Exit For
            End If
        Next PBNames
        .ListIndex = 0
    End With

This is the code I currently use. Is there any way of simplifying it?

I tried this, but to no avail:

Code:
Call PBChoice(DayPB)
Call PBChoice(MidPB)
Call PBChoice(NightPB)



Sub PBChoice(PB As String)

    With Me.PB
            For PBNames = 132 To 141
            If Range("B" & PBNames).Value <> "" Then
            .AddItem Range("B" & PBNames)
            Else: Exit For
            End If
        Next PBNames
        .ListIndex = 0
    End With
    
End Sub

Can anyone tell me the solution?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi,

Try

Code:
Dim vList() As String, n As Long
Dim PBNameList, i As Long

PBNameList = Range("b132:b141")

For i = 1 To UBound(PBNameList, 1)
    If Len(PBNameList(i, 1)) Then
        n = n + 1
        ReDim Preserve vList(1 To n)
        vList(n) = PBNameList(i, 1)
    End If
Next

If n Then
    Me.DayPB.List = vList
    Me.MidPB.List = vList
    Me.NightPB.List = vList
End If

HTH
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,794
Members
452,943
Latest member
Newbie4296

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