在第二张纸上使用列标题在第一张纸上创建列表框时需要一些帮助。我想使用从列表框中选择的值从第二张工作表中删除数据的相关列。我完全迷路了-帮助
答案 0 :(得分:0)
这是VBA解决方案。
Sub PopulateListBox()
Dim ListBox As MSForms.ListBox, columnHeaders As Range, header As Range
Set ListBox = Worksheets("Sheet1").ListBox1
Set columnHeaders = Worksheets("Sheet2").Range("A1:C1")
ListBox.Clear
For Each header In columnHeaders
ListBox.AddItem (header)
Next header
End Sub
Sub ListBox1_Click()
Dim headerIndex As Integer, selectedHeader As Integer
For headerIndex = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(headerIndex) = True Then
DeleteColumn ListBox1.List(headerIndex)
End If
Next headerIndex
PopulateListBox
End Sub
Sub DeleteColumn(headerId As String)
Dim ListBox As MSForms.ListBox, columnHeaders As Range, header As Range
Set columnHeaders = Worksheets("Sheet2").Range("A1:C1")
For Each header In columnHeaders
If header = headerId Then
header.EntireColumn.Delete
End If
Next header
End Sub
注意:
Forms.ListBox
(在我的示例中为sheet1
),并在其中填充列标题(在我的示例中,sheet2
的{{1}}中) li>
A1:C1
中的标题时,该列将被删除,ListBox
更新以将该列作为选择内容删除