我有一个使用以下代码填充的列表框:
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)
我可以调整现有的代码来使其工作吗?
请尝试
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