将列添加到多维列表框

时间:2018-04-20 12:37:01

标签: excel vba excel-vba

我有一个二维数组(从excel中的范围读入),然后我想在我的Userform列表框中显示第1,3,7,8列,如果用户单击按钮列2应该显示,3,6,8,9。

那么使用列宽隐藏列的最佳方法是什么? 如何在不循环的情况下将二维数组放到列表中?

Settings.excelArray = xlApp.Sheets("List").UsedRange.value
MOM.ListBox_Partslist.List = Settings.excelArray

Dim defaultSort() As Integer

defaultSort = Array(2, 3, 4, 5, 6)


'all columns width to 0, how to do?
For i = LBound(defaultSort) To UBound(defaultSort)
    With MOM.ListBox_Partslist
    '.ColumnCount = 54
    '.ColumnWidths = "0;60;0;0...."
...

2 个答案:

答案 0 :(得分:2)

我会这样:

  • 用所有列填充您的列表框

  • 隐藏列表框不受欢迎的列

在你的userfom代码模块中执行此操作:

在最顶层的地方:

Option Explicit

Dim ColumnWidths1 As String, ColumnWidths2 As String
Dim col As Long

作为您的UserForm_Initialize()事件发布此

Private Sub UserForm_Initialize()
    ColumnWidths1 = "50;0;50;0;0;0;50;50;0" ' adapt columns widths to suit your needs
    ColumnWidths2 = "0;50;50;0;0;50;0;50;50" ' adapt columns widths to suit your needs

    Settings.excelArray = xlApp.Sheets("List").UsedRange.value   
    With MOM.ListBox_Partslist
        .ColumnCount = UBound(Settings.excelArray, 2)
        .List = Settings.excelArray
        .ColumnWidths = ColumnWidths1
    End With
    col = 2
End Sub

作为按钮点击事件处理程序放置:

Private Sub CommandButton1_Click() 'change "CommandButton1" to your actual button name
    If col = 1 Then
        MOM.ListBox_Partslist.ColumnWidths = ColumnWidths1
        col = 2
    Else
        MOM.ListBox_Partslist.ColumnWidths = ColumnWidths2
        col = 1
    End If
End Sub

答案 1 :(得分:0)

所以我开始使用稍微修改过的DisplayName解决方案版本。 而不是使用按钮我不得不使用combox:

Public Sub initComboBox()

    Set Settings.filterArray = CreateObject("Scripting.Dictionary")

    Settings.filterArray.CompareMode = vbTextCompare

    Settings.filterArray.Add "MOM VIEW", Split("1,4,5,7,8", ",")
    Settings.filterArray.Add "COST VIEW", Split("1,4,5,7,12", ",")
    Settings.filterArray.Add "FILE VIEW", Split("1,4,5,7,18", ",")
    Settings.filterArray.Add "ALL", ""
    Dim cCont As control
    Dim cName As String


    Set cCont=MOM.ComboBox_FilterCol

    cName = replace(cCont.name, "ComboBox", "Array")

    cCont.List = Settings.comboBoxArray(cName)
    cCont.listIndex = UBound(cCont.List)



End Sub

然后我创建了我的列表框:

MOM.ListBox_Partslist.Clear
Settings.excelArray = xlApp.Sheets("List").UsedRange.value
MOM.ListBox_Partslist.ColumnCount = UBound(Settings.excelArray, 2)
MOM.ListBox_Partslist.List = Settings.excelArray

然后我隐藏了列:

Public Sub changeFilter()
    Dim tempArray() As String
    Dim tempArray2() As Integer
    Dim width As Integer
    Dim columnWidths As String
    Dim i As Integer

    width = 100
    On Error Resume Next
    If MOM.ListBox_Partslist = Null Then Exit Sub

    ReDim tempArray2(UBound(MOM.ListBox_Partslist.List, 2)) As Integer

    columnWidths = ""
    With MOM.ComboBox_FilterCol
        If .value = "ALL" Then
             For i = LBound(tempArray2) To UBound(tempArray2)
                columnWidths = columnWidths & width & ","
            Next i
        Else

            tempArray = Settings.filterArray(.List(.listIndex))

            For i = LBound(tempArray) To UBound(tempArray)
                tempArray2(tempArray(i)) = width
            Next i

            For i = LBound(tempArray2) To UBound(tempArray2)
                columnWidths = columnWidths & tempArray2(i) & ","
            Next i
        End If
        MOM.ListBox_Partslist.columnWidths = Left(columnWidths, Len(columnWidths) - 1)
  End With
End Sub