VBA排序AlphaNumeric

时间:2014-04-24 18:14:19

标签: vba autocad

我在VBA中有一个函数,它应该根据“冒泡排序”对文本进行排序。如果文本只是文本然后它会没问题,但我的文本实际上是一个字母数字字符串。我试图重写它来考虑数字部分,但有些东西仍然关闭,我似乎无法弄清楚是什么。请帮忙!!

Dim alphaCurr As String
Dim alphaNext As String
Dim rowCurr As FsChartRow
Dim rowNext As FsChartRow
Dim c As Integer
Dim n As Integer
Dim vTemp As Variant

For c = 1 To rows.count - 1
    Set rowCurr = rows(c)
    alphaCurr = GetAlpha(rowCurr.label)
    For n = c + 1 To rows.count
        Set rowNext = rows(n)
        alphaNext = GetAlpha(rowNext.label)

        If alphaCurr > alphaNext Then
            Set vTemp = rows(n)
            rows.Remove n
            rows.Add vTemp, , c
        End If

    Next n
Next c

Dim numCurr As Integer
Dim numNext As Integer
Dim loopCount As Integer

    For c = 1 To rows.count - 1
        Set rowCurr = rows(c)
        alphaCurr = GetAlpha(rowCurr.label)
        numCurr = GetNumeric(rowCurr.label)
        For n = c + 1 To rows.count
            Set rowNext = rows(n)
            alphaNext = GetAlpha(rowNext.label)
            numNext = GetNumeric(rowNext.label)

            If alphaCurr = alphaNext Then
                If numCurr > numNext Then
                    Set vTemp = rows(n)
                    rows.Remove n
                    rows.Add vTemp, , c
                End If
            End If

        Next n
    Next c

我得到的结果如下:

“BK1” “BK2” “FB1” “FB4” “FB3” “FB5” “FB6” “FB2” “FJ 2” “FJ1” “FJ3” “FJ4” “......” “FJ15” “RB1” “H1” “H2”

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我仍然无法使冒泡排序起作用,所以我创建了自己的冒泡,不再需要时间来运行冒泡排序。如果有帮助,我会发帖。

Private Function SortFsChartRow(collection As collection) As collection

Dim min As Integer
Dim max As Integer
Dim x As Integer
Dim y As Integer
Dim rowCurr As FsChartRow
Dim numCurr As Integer
Dim rowMin As FsChartRow
Dim rowMax As FsChartRow

Dim search As Integer

Dim sorted As collection
Set sorted = New collection
min = 100
max = 0

For x = 1 To collection.count
    Set rowCurr = collection(x)
    numCurr = GetNumeric(rowCurr.label)
    If numCurr > max Then
        max = numCurr
        Set rowMax = rowCurr
    End If

    If numCurr < min Then
        min = numCurr
        Set rowMin = rowCurr
    End If
Next x

search = min

For y = 0 To max
    For x = 1 To collection.count
        Set rowCurr = collection(x)
        numCurr = GetNumeric(rowCurr.label)
        If numCurr = search Then
            sorted.Add rowCurr
            Exit For
        End If
    Next
    search = search + 1
Next y

Set SortFsChartRow = sorted

结束功能