如何仅使用工作表中的某些列填充列表框?

问题描述 投票:0回答:1

我有一个使用以下代码填充的列表框:

Private Sub Userform_initialize()

Dim ws As Worksheet
Dim Rng As Range
Dim LRow As Long
Dim Myarray As Variant

 Set ws = ThisWorkbook.Sheets(Mark1.LEADLISTDROPDOWN.Value)
 
 With ws
 LRow = .Range("BJ" & .Rows.Count).End(xlUp).row
 
 Set Rng = ws.Range("A2:KH" & LRow)
 
PopoutLeadListBox.RowSource = Rng.Address(External:=True)
 
 End With
 
End Sub

但是,我想将其调整为仅加载某些列,特别是列(1, 2, 3, 4, 5, 6, 7, 8, 9, 52, 61, 62, 63, 64, 65, 66 、 124、 145、 221、 234、 247、 294)

我可以调整现有的代码来使其工作吗?

excel vba listbox
1个回答
0
投票

请尝试

Private Sub Userform_initialize()
    Dim ws As Worksheet
    Dim Rng As Range
    Dim LRow As Long
    Dim arrCol, i, j, arrList
    arrCol = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 52, 61, 62, 63, 64, 65, 66, 124, 145, 221, 234, 247, 294)
    Set ws = ThisWorkbook.Sheets(Mark1.LEADLISTDROPDOWN.Value)
    With ws
        LRow = .Range("BJ" & .Rows.Count).End(xlUp).Row
        ReDim arrList(1 To LRow, UBound(arrCol))
        For i = 1 To LRow
            For j = 0 To UBound(arrCol)
                arrList(i, j) = ws.Cells(i, arrCol(j)).Value
            Next
        Next
        Me.PopoutLeadListBox.ColumnCount = UBound(arrCol) + 1
        Me.PopoutLeadListBox.List = arrList
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.