gpt4 book ai didi

vba - ListBox 基于 ComboBox 填充 - 不同的数据结构

转载 作者:行者123 更新时间:2023-12-04 20:58:38 26 4
gpt4 key购买 nike

进入选项卡“设置”我有自动生成的工作表列表:

enter image description here

栏目格式 : 带有 的床单格式 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
ETC....

最佳答案

我做了如下:

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

但是,如果您知道我怎样才能做得更好或采用不同的方式,请与我分享这个想法。

关于vba - ListBox 基于 ComboBox 填充 - 不同的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41546566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com