2017-01-09 24 views
2

進入選項卡「設置」我已經自動生成張清單:列表框填入 - 不同的數據結構

enter image description here

列格式:與格式中的牀單看起來是一樣的,只包含不同的值和全部與格式B看起來也一樣,包含再一次不同的值。

現在我添加值組合框:

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 

然後我想基於值來填充listBox2點擊CB3。但下面的代碼對所有格式都是一樣的。

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 

請幫忙。

格式A: 範圍:A8:H100

格式B: 範圍:B10:G50

格式C: 範圍

不同勢範圍的基礎上,格式的實施例:C20:B30 etc ....

+0

你能告訴我們輸出*是什麼*與它*應該是什麼*? – CallumDA

+0

請參閱不同範圍的示例 – 4est

+1

所有工作表得到相同結果的原因是因爲在嘗試佔用具有'.Range(.Range(「B6」),.Range(「B6」)的'NameArray'數組後, .End(xlDown))',你**「覆蓋」**下一行,用'NameArray = .Range(「A8:H100」)'(它總是相同的大小) –

回答

0

我做了如下:

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 

但是,如果您知道如何更好地或以不同的方式做到這一點,請與我分享這個想法。

相關問題