Avatar of EBocchieri
EBocchieri
 asked on

filtered data as source of a listbox with VBA

I have a Range containg data. Using Excel VBA, I would filter data accordling a criteria and display only the filtered data in a listbox . Can you help me?
Microsoft OfficeOffice ProductivitySpreadsheets

Avatar of undefined
Last Comment
EBocchieri

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
paisleym

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
EBocchieri

ASKER
Hi Marcelle, thanks for your replay. I haven't try your code but it seems properly working. My request was, specifically, for use a range filtered in place, where height of the rows is =0 and are not "visible" ,when data are not according with the criteria selected:
Your replay obviously was suffucient for my request, but if you know a code for the above request, I please you to send me it, Thank you very mauch for yor courtesy
Sincerely, Emanuele
paisleym

Ok, "where height of the rows is =0 and are not "visible"" was not in the original request. I have not tried to do this myself, but

Me.lstLines.Visible = false

should work.
EBocchieri

ASKER
Hi Marcelle,
I have tried this code and is properly working, many thanks
Have you a nice day
Sincerely, Emanuele



Private Sub CommandButton1_Click()                          'is a button in Sheet("DB")
'assuming duplicate values are possible.
'Use macro record to sort the area be the listbox value.
ThisWorkbook.Sheets("DB").Select
oldValue = ""
For r = 2 To 65536 ' declare r as long, assuming focus is already on Activesheet
  If IsEmpty(Cells(r, 1)) = True Then ' where 1 is the column sorted on
     Exit For
  End If
  If Cells(r, 1) <> oldValue Then
     If Cells(r, 1).Height > 0 Then  'Autofilter gives Height = 0 for record not matching
       Me.lstLines.AddItem (Cells(r, 3).Value) ' Cells(r.., not s) lstLines is the name of the listbox.
       oldValue = Cells(r, 1)
     End If
  End If
Next r
End Sub
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck