VBA code to link combo Box & List Box by combo box selection list box data to beshown

Hema

New member
Joined
Oct 17, 2017
Messages
4
Reaction score
0
Points
0
VBA code to link combo Box & List Box by combo box selection list box data to beshown

Hello,

I am trying to display excel data in user form listbox . Listbox data to be filled with the combobox date selection.

Kindly suggest me a vba code to link Combobox and listbox and to display excel data in list box.

Please find the below table which is excel data , and to be displayed in listbox.

LineDate/TimeShiftAssociate NameFamilyPart Number
AAA3/8/2017 10:151DDDAAAAAA56767895678
BBB3/8/2017 10:422CCCBBBBBBB3456745678
 
Welcome to the forum!

Without knowing details like what column to filter by and what sheet and range to filter, it is hard to help. You can attach short example files by clicking the Go Advanced button in lower right of a reply box. Click the paperclip icon in toolbar or Manage Attachments below reply box.

In this example file, I filtered by column A on sheet 1. The usual method that some use is to use a scratch sheet or range on the activesheet to paste filter results. The method I used was to filter and sort arrays. I also use a routine to copy a range without possible hidden rows to an array. It is not fool-proof but works for me in this example file.
 

Attachments

  • FilterByComboBox.xlsm
    33.7 KB · Views: 528
excel userform attached

Hello Hobson,

Thanks for the reply.

Expected to have this filter in userform directly whithout adding an additional button in excel sheet.

Please find the attached excel file which I am creating an userform everything is ok.

But displaying the excel data in userform listbox is not happening by filtering in combobox.

Reference column for filterning should be date Column B.

Please help me to display the data in listbox by combobox filterning.
 

Attachments

  • AOI Data Tracking Sheet.xlsm
    48.4 KB · Views: 247
I had some issues dealing with your example file. I could give you some suggestions to improve readability, maintenance, and maybe speed, and generally coding if you like.

As for the button issue, there are many ways to Show a userform. My button was just one way.

You should be able to understand this 2nd version of my file. Since datetimes in column B are unique, filling Combobox1's List is much easier. No sorting or checking for duplicates as I did for column A appears to be needed. Since it is a datetime value, autofilter is a bit more tricky.

I still used my copy range to array method to fill the listbox list on ComboBox1_Change. As such, either copy/paste mMain module routines or just drag and drop mMain in Visual Basic Editor's (VBE's) Project Explorer window. If not in view in VBE, click VBE's View menu item to enable. Then drag and drop the Module from my VBA project to yours.

Lastly, copy and paste the uf2's code for Initialize and Combobox_Change to yours. It should then work in your project.

For those that don't want to open the file:
Code:
'Module
'Returns variant array Base 1.
Function StringTo2dArray(s As String, Optional rowDelim = vbCrLf, Optional colDelim = vbTab)
  Dim i As Long, j As Integer, a, aa, b, c As Integer, r As Long
  a = Split(s, rowDelim)
  'Trim empty last element if needed. e.g. Range copied to clipboard as s.
  If Len(a(UBound(a))) - Len(Replace(a(UBound(a)), vbTab, "")) = 0 Then _
    ReDim Preserve a(0 To UBound(a) - 1)
    r = UBound(a) + 1
  c = UBound(Split(a(0), colDelim)) + 1
  ReDim b(1 To r, 1 To c)
  For i = 1 To r
    aa = Split(a(i - 1), colDelim)
    For j = 1 To c
      b(i, j) = aa(j - 1)
    Next j
  Next i
  StringTo2dArray = b
End Function


Function GetClipboard()
  'Early bound MSForms.DataObject
  'Tools > References > Microsoft Forms 2.0 Object Library
  'Dim MyData As DataObject
  
  'Late bound MSForms.DataObject
  Dim MyData As Object
  Set MyData = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")


  On Error Resume Next
  Set MyData = New DataObject
  MyData.GetFromClipboard
  GetClipboard = MyData.GetText
End Function


'uf2
Private Sub UserForm_Initialize() 
  With Worksheets("AOIData")
    ListBox1.ColumnCount = .UsedRange.Columns.Count
    ListBox1.List = .UsedRange.Value
    ComboBox1.List = .Range("B2", .Cells(.Rows.Count, "B").End(xlUp)).Value
  End With


  ComboBox1.AddItem ""
End Sub


Private Sub ComboBox1_Change()
  Dim a
  With Worksheets("AOIData")
    If ComboBox1.Value = "" Then
      ListBox1.List = .UsedRange.Value
      Exit Sub
    End If
    
    .UsedRange.AutoFilter 2, Format(CDate(ComboBox1.Value), "m/d/yyyy h:mm")
    .UsedRange.SpecialCells(xlCellTypeVisible).Copy
    a = StringTo2dArray(GetClipboard)
    Application.CutCopyMode = False
    ListBox1.List = a 'WorksheetFunction.Transpose(a)
  End With
End Sub


Private Sub CommandButton1_Click()
  Application.CutCopyMode = False
  With Worksheets("AOIData")
    If .AutoFilterMode Then .UsedRange.AutoFilter
  End With
  Unload Me
End Sub
 

Attachments

  • FilterByComboBox2.xlsm
    41.2 KB · Views: 395
Last edited:
AOI Tracker

Thanks for your reply I have added your code in excel but filtering is not happening properly could you please help to solve the issue.

I have attached the excel sheet hope this time u will not face issue in using my excel sheet..Or if u have any other ideas u can suggest me.

And also can we taken 2 columns as reference to filter the data for example in my excel I have Date & Shift column can I filter the data based on 2 columns.

Thanks..
 

Attachments

  • AOI Data Tracking Sheet (1).xlsm
    63.8 KB · Views: 295
Back
Top