简体   繁体   English

Excel VBA数组中2D Bubblesort上的运行时错误

[英]Runtime Error on a 2D Bubblesort in Excel VBA array

I have been banging my head (and a few other heads as well on other Excel programming sites) to get a Combobox in a Userform to sort the rows (coming from two columns in the source spreadsheet) in alpha order. 我一直在努力工作(在其他Excel编程网站上也是如此),以获取用户窗体中的组合框,以按字母顺序对行(来自源电子表格中的两列)进行排序。

Ideally, I want a 2 dimensional sort, but at this point, will settle for ONE that works. 理想情况下,我想要一个二维排序,但在这一点上,它将适合一个可行的方法。

Currently, the Combobox, when dropped down, reads in part (minus the bullet points, which do NOT appear and are not needed): 当前,组合框在下拉时会部分读取(减去项目符号要点,这些要点不会出现并且不需要):

  • Zoom MRKPayoutPlan 放大MRKPayoutPlan
  • Chuck PSERSFuture Chuck PSERS未来
  • Chuck PSERSCurrent Chuck PSERS当前

What I want is: 我想要的是:

  • Chuck PSERSCurrent Chuck PSERS当前
  • Chuck PSERSFuture Chuck PSERS未来
  • Zoom MRKPayoutPlan 放大MRKPayoutPlan

The first order is derived from the order in which the rows appear in the source worksheet. 第一顺序是从行在源工作表中出现的顺序得出的。

At this point, I am getting a Runtime Error '13', Type Mismatch error. 此时,我收到运行时错误'13',类型不匹配错误。 Both fields are text fields (one is last name, the other is a classification code- I want to sort first by name). 这两个字段都是文本字段(一个是姓氏,另一个是分类代码-我想先按名称排序)。

Below are the two relevant sections of the VBA code. 以下是VBA代码的两个相关部分。 If someone can help me sort this out, I'll buy at least a virtual round of beers. 如果有人可以帮助我解决这个问题,我将至少购买虚拟啤酒。 Excel VBA is not my most comfortable area- I can accomplish this in other apps, but the client spec is that this all must run in Excel alone. Excel VBA不是我最舒适的领域-我可以在其他应用程序中完成此操作,但是客户端规范要求所有这些都必须单独在Excel中运行。 Thanks in advance. 提前致谢。

Private Sub UserForm_Initialize()
   fPath = ThisWorkbook.Path & "\"
   currentRow = 4

   sheetName = Sheet5.Name
   lastRow = Sheets(sheetName).Range("C" & Rows.Count).End(xlUp).Row


    Dim rngUID As Range
    Dim vList

    Set rngUID = Range("vUID")

    With rngUID
        vList = Application.Index(.Cells, .Parent.Evaluate("ROW(" & .Address & ")"), Array(7, 1))
    End With
   vList = BubbleSort2D(vList, 2, 1)

    With ComboBox1
        .ColumnCount = 2
        .ColumnWidths = "100;100"
        .List = vList
    End With

   PopulateControls
End Sub

Public Function BubbleSort2D(Strings, ParamArray SortColumns())
    Dim tempItem
    Dim a                     As Long
    Dim e                     As Long
    Dim f                     As Long
    Dim g                     As Long
    Dim i                     As String
    Dim j                     As String
    Dim m()                   As String
    Dim n
    Dim x As Long
    Dim y As Long
    Dim lngColumn As Long


    e = 1
    n = Strings
    Do While e <> -1

        For a = LBound(Strings) To UBound(Strings) - 1
            For y = LBound(SortColumns) To UBound(SortColumns)
                lngColumn = SortColumns(y)
                i = n(a, lngColumn)
                j = n(a + 1, lngColumn)
                f = StrComp(i, j)
                If f < 0 Then
                    Exit For
                ElseIf f > 0 Then
                    For x = LBound(Strings, 2) To UBound(Strings, 2)
                        tempItem = n(a, x)
                        n(a, x) = n(a + 1, x)
                        n(a + 1, x) = tempItem
                    Next x
                    g = 1
                    Exit For
                End If
            Next y
        Next a
        If g = 1 Then
            e = 1
        Else
            e = -1
        End If

        g = 0
    Loop
    BubbleSort2D = n
End Function

Here is a bubble sort in VBA source . 这是VBA 源代码中的冒泡排序。

Public Sub BubbleSort(ByRef sequence As Variant, _
        ByVal lower As Long, ByVal upper As Long)

    Dim upperIt As Long
    For upperIt = upper To lower + 1 Step -1

        Dim hasSwapped As Boolean
        hasSwapped = False

        Dim bubble As Long
        For bubble = lower To upperIt - 1

            If sequence(bubble) > sequence(bubble + 1) Then

                Dim t as Variant
                t = sequence(bubble)

                sequence(bubble) = sequence(bubble + 1)
                sequence(bubble + 1) = t
                hasSwapped = True

            End If

        Next bubble

        If Not hasSwapped Then Exit Sub

    Next upperIt

End Sub

Note that using variable names that specify what they are and do instead of single letters makes it easier to read. 请注意,使用变量名称而不是单个字母来指定它们的作用和用途,这样更易​​于阅读。

As for the 2D sort. 至于2D排序。 Don't. 别。 Sort each array individually then sort the array of arrays using the same method. 对每个数组分别进行排序,然后使用相同的方法对数组进行排序。 You will need to provide an abstraction to compare the columns. 您将需要提供抽象来比较各列。 Do not try to sort them both at the same time. 不要尝试同时对它们进行排序。 I can't think of a scenario where that is a good idea. 我想不出一个好主意的方案。 If for some reason elements can change their sub array in the 2D array, then flatten it into 1 array, sort that and split it back into a 2D array. 如果由于某些原因元素可以更改2D数组中的子数组,则将其展平为1个数组,对其进行排序并将其拆分回2D数组。

Honestly from what I am understanding of you specific problem. 老实说,据我了解您的具体问题。 You are going from 1D sequence to a 1D sequence so I think 2D arrays are and unnecessary complication. 您正在从1D序列转到1D序列,所以我认为2D数组是不必要的复杂化。

Instead use a modified bubble sort routine with the comparison statement, 而是将修改后的冒泡排序例程与比较语句一起使用,

 If sequence(bubble) > sequence(bubble +1) Then '...

replaced with a custom comparison function 替换为自定义比较功能

ComboBoxItemCompare(sequence(bubble), sequence(bubble + 1))

that will return True if the first argument should be swapped with the second. 如果第一个参数应与第二个参数交换,则将返回True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM