【问题标题】:Sort Combobox alphabetically vba excel按字母顺序排序组合框vba excel
【发布时间】:2019-05-29 07:35:57
【问题描述】:

我正在用 vba Excel 构建一个小程序。我的 Comboxbox 是从 excel 表中填充的。我想构建一些控件,例如“添加/删除项目”,以将其他项目添加到我的组合框中,这就是为什么我需要知道组合框中每个项目的行号。我的代码能够做到这一点,但我仍然不知道如何在将项目添加到组合框之前按字母顺序对项目进行排序(请参阅下面的代码)你能帮忙吗?在此先感谢

Sub Fill_EightD_D1_CB1() With EightD.EightD_D1_CB1 .ColumnCount = 2 ' 2 colonnes .ColumnWidths = "-1;0" ' dont une de masquée End With Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("D1") Dim LC As Long Dim i As Long LC = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row For i = 2 To LC If ws.Cells(i, 1) <> "" Then EightD.EightD_D1_CB1.AddItem ws.Cells(i, 1).Value EightD.EightD_D1_CB1.List(EightD.EightD_D1_CB1.ListCount - 1, 1) = Mid(ws.Cells(i, 1).Address(False, False), 2, 1) End If Next i 'show always the first element EightD.EightD_D1_CB1.ListIndex = 0 'Bold Text EightD_D1_CB1 EightD.EightD_D1_CB1.Font.Bold = True End Sub

【问题讨论】:

    标签: excel vba sorting combobox


    【解决方案1】:

    试试

    Sub Fill_EightD_D1_CB1()
    
        With EightD.EightD_D1_CB1
            .ColumnCount = 2        ' 2 colonnes
            .ColumnWidths = "-1;0"  ' dont une de masquee
    
        End With
        Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("D1")
        Dim LC As Long
        Dim i As Long, r As Long, j As Long
        Dim vDB As Variant, vR(), vtemp(1 To 2)
    
        LC = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        vDB = ws.Range("a2", "a" & LC)
        r = UBound(vDB, 1)
        ReDim vR(1 To r, 1 To 2)
        For i = 1 To r
            vR(i, 1) = vDB(i, 1)
            vR(i, 2) = i + 1
        Next i
        For i = 1 To r
            For j = 1 To r
                If vR(i, 1) < vR(j, 1) Then
                    vtemp(1) = vR(i, 1)
                    vtemp(2) = vR(i, 2)
                    vR(i, 1) = vR(j, 1)
                    vR(i, 2) = vR(j, 2)
                    vR(j, 1) = vtemp(1)
                    vR(j, 2) = vtemp(2)
                End If
            Next j
        Next i
        EightD.EightD_D1_CB1.List = vR
        'show always the first element
        EightD.EightD_D1_CB1.ListIndex = 0
        'Bold Text EightD_D1_CB1
        EightD.EightD_D1_CB1.Font.Bold = True
    End Sub
    

    【讨论】:

    • 感谢@Dy.Lee 的回答我在excel 中使用了xlFilterinplace 并解决了问题。我读了你的答案,它似乎正在工作。我会把它设置为正确的答案。
    【解决方案2】:

    使用支持排序的数据结构首先捕获数据,对其进行排序,然后添加到您的列表框。我已经在下面的代码中展示了如何添加和排序。

    Sub SortAnArrayList()
        Dim ArrayList As Object
        Dim ArrayItem As Variant
    
        Set ArrayList = CreateObject("System.Collections.ArrayList")
    
        With ArrayList
            .Add "b"
            .Add "c"
            .Add "a"
            .Sort
        End With
    
        For Each ArrayItem In ArrayList
            Debug.Print ArrayItem
        Next
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-26
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多