How to use a variable to represent a combo box object in VBA?

jpug 20 Reputation points
2023-10-20T00:16:50.69+00:00

Can't figure out how to use a variable for a combo box in the following code. Goal is to get the loop to loop through the combo box names and check if they are empty. Had this functioning with an Array of all the combo box names.

Private Sub PrelimPermitSub_Click()


Dim Counter As Long


Counter = 1


    Do While Counter < 16
        Dim yy As String
        yy = "CB" & Counter
        MsgBox (yy)
       
        
        
        
        If ww.Value = "" Then
            MsgBox ("Please complete forms in all tabs.")
        Exit Do

        Else
            Dim cnum As String
            cnum = 30 + Counter
            cell = "D" + cnum
            Sheets("Backend").Range(cell).Value = ww.Value
        End If

        Counter = Counter + 1
        Loop

End Sub
Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-10-20T05:25:47.95+00:00

    In case of User Forms, check an example:

    Dim cb As ComboBox
    Set cb = Controls("CB1") ' or Controls("CB" & Counter)
    
    Dim v As String
    v = cb.Value
    
    MsgBox v
    

    If it works, integrate it to your loop.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.