loop through listbox with condition

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
i need simple code when i show listbox in userform i would only data contain expired
i have sheet1 contains data 6 columns begin from a2:f200 the column 6 (f) contains expired when i run userform it show data only expired in listbox
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
How about
VBA Code:
Private Sub UserForm_Initialize()
    Dim Ary As Variant, Nary As Variant
    Dim r As Long, c As Long, nr As Long
    
    With Sheets("sheet1")
        nr = Application.CountIf(.Range("F:F"), "Expired")
        If nr = 0 Then Exit Sub
        Ary = .Range("A1").CurrentRegion.Value2
    End With
    ReDim Nary(1 To nr, 1 To UBound(Ary, 2))
    nr = 0
    For r = 2 To UBound(Ary)
        If LCase(Ary(r, 6)) = "expired" Then
            nr = nr + 1
            For c = 1 To UBound(Ary, 2)
                Nary(nr, c) = Ary(r, c)
            Next c
        End If
    Next r
    Me.ListBox1.List = Nary
End Sub
 
Upvote 0
Dim Ary As Variant, Nary As Variant Dim r As Long, c As Long, nr As Long With Sheets("sheet1") nr = Application.CountIf(.Range("F:F"), "Expired") If nr = 0 Then Exit Sub Ary = .Range("A1").CurrentRegion.Value2 End With ReDim Nary(1 To nr, 1 To UBound(Ary, 2)) nr = 0 For r = 2 To UBound(Ary) If LCase(Ary(r, 6)) = "expired" Then nr = nr + 1 For c = 1 To UBound(Ary, 2) Nary(nr, c) = Ary(r, c) Next c End If Next r Me.ListBox1.List = Nary
it gives me error runtime error 70 premission denied
 
Upvote 0
What line gave the error?
 
Upvote 0
sorry for give me the rowsource causes the error now the code is great thanks so much
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,800
Members
449,127
Latest member
Cyko

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