【发布时间】:2019-12-17 17:27:30
【问题描述】:
我有一个 excel 文件,我在其中从第 2 行动态加载列标题,直到我命中一个空值并将所有这些值放入一个列表框中转置。这部分正在按我的预期工作。
我的问题是,如何使用列表框项来选择命名标题所在的整个列?
所以在 A2 B2 C2 中,我将名为 Widget 1、2、3 的标题分别加载到列表框中。当用户窗体加载时,这些在列表框中按顺序加载。在列表框中,我希望能够单击小部件 2 和 3 并选择这些整列。我不希望它被硬编码,因为我希望它是一个选择,因为我可以选择小部件 1 和 3 或任何需要的随机选择。
我可以有多达 50 个小部件....所以这些小部件都会在启动时加载到列表框中,我需要能够选择任何这些值并让它们选择相应的列....
这就是我遇到问题的地方,如何进行多选。
提前感谢您的帮助。
编辑:
这是 Private Sub UserForm_Initialize() 上使用的代码
'Figure out how many actual columns headers there are and then search for signal names
'Dim Lastcol As String
Dim FoundColumnRangeCalculated As Variant
Dim Lastcol As Variant
Dim FoundColumnRange As Variant
With ActiveSheet
Lastcol = .Cells(2, .Columns.Count).End(xlToLeft).Column
''MsgBox Lastcol
End With
'Convert numerical column location to letter value to use as dynamic range header lookup
Dim NumberToColumn As Variant
Dim SearchColumn
SearchColumn = Lastcol
NumberToColumn = Left(Cells(1, SearchColumn).Address(1, 0), InStr(1, Cells(1, SearchColumn).Address(1, 0), "$") - 1)
'MsgBox NumberToColumn
'Build the actual range from found column headers
FoundColumnRangeCalculated = "A2:" & (NumberToColumn & Lastcol)
'Transfer headers vertically to the list box for user to see
ListBox1.List = Application.WorksheetFunction.Transpose(Range(FoundColumnRangeCalculated))
Imported List Box Transposed on UserForm Load
所以现在当我单击或多次单击(未显示在此图像中,但多次单击要启用的选择选项)项目时,我希望在单击每个项目时选择它所代表的相应列。
Columns that the items are drawn from
由于它们是按从左到右的“顺序”构建的,我假设它是 1:1 匹配和搜索和选择,但我在尝试对其中的那部分进行排序时遇到了麻烦......很多关于获取它的数据,解析等.....我只是只需要一个“选择listbox项目时,使用选择以启用它的列”。
列不能针对范围进行硬编码,因为标题可能是 A:F、A:AA 或 A:ZZ.......所以它必须是动态匹配。
感谢那些回复的人,希望这个编辑过的帖子和图片能满足搁置状态。
【问题讨论】:
-
你已经尝试过的代码在哪里?
-
另外,数据截图会更容易理解。
-
我已经编辑了帖子并添加了图片,希望这能澄清我最初的询问