ListBox populate based on ComboBox - different data structure

696 views Asked by At

Into tab "setting" I have sheets list generated automatically:

enter image description here

Column format: sheets with format A looks the same, contains only diffrent values and all with format B looks also the same, contains once again diffrent values .

Now I'm adding values to combobox:

Dim db_rows As Long, i As Long
Dim wbs As Workbook
Dim wss As Worksheet

Set wbs = ActiveWorkbook
Set wss = wbs.Worksheets("setting")

db_rows = wss.Cells(Rows.Count, 1).End(xlUp).Row

With cb3
    .Clear
        For i = 2 To db_rows
            If wss.Cells(i, 1).Value <> "" Then
                .AddItem wss.Cells(i, 2).Value
            End If
        Next i
End With
Me.cb3.ListIndex = 0

Then I want to populate listBox2 based on value clicked on cb3. But below code is working that same for all format.

Dim NameArray As Variant
With Sheets(Me.cb3.Value)
    NameArray = .Range(.Range("B6"), .Range("B6").End(xlDown))
    NameArray = .Range("A8:H100")
End With
listBox2.List =  NameArray

Please help.

Example of diffrent ranges, based on Format:

Format A: Range: A8:H100

Format B: Range: B10:G50

Format C: Range: C20:B30 etc....

1

There are 1 answers

0
4est On BEST ANSWER

I did as below:

Dim j, wss1, db_x As Long
Dim selected_value, format_db As String
wss1 = wss.Cells(wss.Rows.Count, "B").End(xlUp).Row
selected_value = Me.cmbListItem2.Value

Dim NameArray3 As Variant
Dim col_numA, col_numB As Integer
col_numA = 6
 col_numB = 3

   For j = 2 To wss1
       If wss.Range("B" & j).Value = selected_value Then
           format_db = wss.Range("C" & j).Value
           Exit For
       End If
   Next

   Select Case format_db
        Case "A"        
                With Sheets(Me.cmbListItem2.Value)
                db_x = .Cells(.Rows.Count, "A").End(xlUp).Row
                NameArray3 = .Range("A8:H" & db_x)
                End With

                With listBox2
                    .ColumnCount = col_numA
                    .List = NameArray3
                    .ColumnWidths = "50;200;50;50;50;50"
                    .MultiSelect = fmMultiSelectMulti
                End With

        Case "B"
                With Sheets(Me.cmbListItem2.Value)
                db_x = .Cells(.Rows.Count, "B").End(xlUp).Row
                NameArray3 = .Range("B10:G" & db_x)
                End With

                With listBox2
                    .ColumnCount = col_numB
                    .List = NameArray3
                    .ColumnWidths = "50;50;50;50;50;50"
                    .MultiSelect = fmMultiSelectMulti
                End With                
        Case Else            
    End Select

But if you know how can I do it better or into diffrent way please share the idea with me.