Gisse
Gisse

Reputation: 49

Separate listbox items to do a for each loop

I'm having trouble seperating my listbox items, so I can run them in a loop. I know this atm takes all items and tries to run them through, however I don't know how to seperate them. The code is following:

Dim SelectedItems As String
Dim LastRow As Long

LastRow = ActiveSheet.Range("F1").SpecialCells(xlCellTypeLastCell).Row

For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) = True Then
SelectedItems = SelectedItems & ListBox2.List(i) & vbNewLine
End If
Next i

If SelectedItems = "" Then
MsgBox "Please select minimum one country"
Else

For Each SelectedItems In ListBox2

    For i = 11 To LastRow

    If Range("F" & i).Value = SelectedItems Then
    Rows(i).EntireRow.Hidden = True
    Else: Rows(i).EntireRow.Hidden = False
    End If

    Next i
Next SelectedItems

can someone assist?

Upvotes: 2

Views: 328

Answers (2)

FaneDuru
FaneDuru

Reputation: 42236

Dim SelectedItems As String, LastRow As Long
Dim selItem As Variant, selItems As Variant

LastRow = ActiveSheet.Range("F1").SpecialCells(xlCellTypeLastCell).Row

For i = 0 To ListBox2.ListCount - 1
    If ListBox2.Selected(i) = True Then
        SelectedItems = SelectedItems & ListBox2.List(i) & vbNewLine
    End If
Next i

Stop
If SelectedItems = "" Then
    MsgBox "Please select minimum one country"
Else
    SelectedItems = left(SelectedItems, Len(SelectedItems) - 1)
    selItems = Split(SelectedItems, vbNewLine)
    For Each selItem In selItems
        For i = LastRow To 11 Step -1
            If CStr(Range("F" & i).value) = CStr(selItem) Then
                Stop
                Rows(i).EntireRow.Hidden = True
                'Else: Rows(i).EntireRow.Hidden = False
            End If
        Next i
    Next
End If

Your code could not identify each selected Item from the string without splitting it in its elements. Edited: Transformed the code in a test one. I will explain you (in a comment) how to check.

Note: I just tried to make your code workable. Otherwise, you can make the filtering directly, using Excel AutoFilter (in VBA, of course)...

Upvotes: 1

JvdV
JvdV

Reputation: 75840

So just a small example on how I would approach this, avoiding multiple loops:


Sample data:

enter image description here


UserForm Sample:

enter image description here


UserForm Sample Code:

Option Explicit

Private Sub CommandButton1_Click()

Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim x As Long, lr As Long
Dim rng As Range

'Check if anything has been selected at all
If Me.ListBox1.ListIndex = 0 Then Exit Sub

'Capture selected items in your ListBox
For x = 0 To Me.ListBox1.ListCount - 1
    If Me.ListBox1.Selected(x) Then
        dict(ListBox1.List(x)) = 1
    End If
Next x

'Filter the range accordingly
With Sheet1
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    Set rng = .Range("A1:A" & lr)
    rng.AutoFilter 1, Array(dict.keys), xlFilterValues
End With

End Sub

Private Sub UserForm_Initialize()

Dim lr As Long

'Populate your ListBox
With Sheet1
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    Me.ListBox1.RowSource = .Range("A2:A" & lr).Address
End With

End Sub

Sample Result:

enter image description here enter image description here

Upvotes: 1

Related Questions