ListBox 基于 ComboBox 填充 - 不同的数据结构
ListBox populate based on ComboBox - different data structure
进入选项卡"setting"我有自动生成的工作表列表:
列格式:具有格式 A 的工作表看起来相同,仅包含不同的值,并且所有具有 格式 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
然后我想根据在 cb3 上单击的值填充 listBox2。但是下面的代码对所有格式都是一样的。
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
等....
我做了如下操作:
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
但如果你知道我怎样才能做得更好或采用不同的方式,请与我分享这个想法。
进入选项卡"setting"我有自动生成的工作表列表:
列格式:具有格式 A 的工作表看起来相同,仅包含不同的值,并且所有具有 格式 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
然后我想根据在 cb3 上单击的值填充 listBox2。但是下面的代码对所有格式都是一样的。
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 等....
我做了如下操作:
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
但如果你知道我怎样才能做得更好或采用不同的方式,请与我分享这个想法。