Help with For loop

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
625
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
On my userform I have 2 listboxes and 25 Comboboxes each contain a list of the same people's name. What I need to happen is when a user selects a name from any of the listboxes or comboxes it deletes that value from all the comboboxes and listboxes. I bolded the text that doesn't work. ctrl always equals nothing and does not run the next line of code. Thank you.

VBA Code:
Private Sub ListBox1_Change()
   On Error GoTo myExitSub

    Dim Res As Variant
    Dim i As Integer, l As Integer, numselections As Integer
    Dim PTO As Range
    Dim ctrl As Controls
        
'Loop through every item in the ListBox
For i = 0 To ListBox1.ListCount - 1

    'Check if the item was selected.
    If ListBox1.Selected(i) Then
    
        numselections = numselections + 1
     
'find position of 'ListBox1.Selected(i)' in listbox items
        Res = Application.Match(ListBox1.List(i), ListBox1.List, 0)
        
        For Each PTO In ActiveSheet.Range("B17:D17").Cells
            
            If Len(PTO) = 0 Then
                PTO.Select
                Debug.Print ActiveCell.Address
                If ActiveCell.Address > "$C$17" Then GoTo myExitSub
                ActiveCell.Value = ListBox1.List(i)
                Exit For
            End If
        
        Next
      
        If Not IsError(Res) Then
            [B]For Each ctrl In Me.Controls
                If TypeName(ctrl) = "Listbox" Or "Combobox" Then
                    ctrl.RemoveItem Res - 1 ' subtract 1 because the index of is zero-based.
                End If
            Next ctrl[/B]
        End If
        
    End If

Next i


For l = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(l) = True Then
        numselections = numselections + 1
    End If
Next l
MsgBox numselections



myExitSub:
    Exit Sub
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Instead of this:
VBA Code:
If TypeName(ctrl) = "Listbox" Or "Combobox" Then

Try this:
VBA Code:
If TypeName(ctrl) = "Listbox" Or TypeName(ctrl) = "Combobox" Then
 
Upvote 0
VBA Code:
Ctrl still equals "Nothing"

Not when I try it.
VBA Code:
            If Not IsError(Res) Then
                Dim CType As String
                For Each ctrl In Me.Controls
                    CType = TypeName(ctrl)
                    Select Case CType
                    Case "ListBox", "ComboBox"
                        ctrl.RemoveItem Res - 1       ' subtract 1 because the index of is zero-based.
                    End Select
                Next ctrl
            End If
 
Upvote 0
Ctrl still equals "Nothing". What am I missing?

VBA Code:
Private Sub ListBox1_Change()
   On Error GoTo myExitSub

    Dim Res As Variant
    Dim i As Integer, l As Integer, numselections As Integer
    Dim PTO As Range
    Dim ctrl As Controls
        
'Loop through every item in the ListBox
For i = 0 To ListBox1.ListCount - 1

    'Check if the item was selected.
    If ListBox1.Selected(i) Then
    
        numselections = numselections + 1
        'MsgBox numselections

        'find position of 'ListBox1.Selected(i)' in listbox items
        Res = Application.Match(ListBox1.List(i), ListBox1.List, 0)
        
        For Each PTO In ActiveSheet.Range("B17:D17").Cells
            
            If Len(PTO) = 0 Then
                PTO.Select
                Debug.Print ActiveCell.Address
                If ActiveCell.Address > "$C$17" Then GoTo myExitSub
                ActiveCell.Value = ListBox1.List(i)
                Exit For
            End If
        
        Next
      
        If Not IsError(Res) Then
                Dim CType As String
                For Each ctrl In Me.Controls
                    CType = TypeName(ctrl)
                    Select Case CType
                        Case "ListBox", "ComboBox"
                            ctrl.RemoveItem Res - 1       ' subtract 1 because the index of is zero-based.
                    End Select
                Next ctrl
        End If
    End If

Next i


For l = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(l) = True Then
        numselections = numselections + 1
    End If
Next l
MsgBox numselections

myExitSub:
    Exit Sub
 
Upvote 0
So if I change this "Dim ctrl As Controls" to "Dim ctrl As Control" would I not also need to do something to this line of code?
"For Each ctrl In Me.Controls" Thank you.
 
Upvote 0
No, that line must remain the same.
 
Upvote 0
"Control" is an object > Singular > Each (singular) ctrl
"Controls" is a collection > Plural > Me.Controls (all the controls of the Listbox)
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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