4

我有一个排序后看起来像这样的数据集:

Abc0
Abc100
Abv15
Abc4
Abc700

我想知道我怎样才能使它像这样排序:

  Abc0
  Abc4
  Abc15
  Abc100
  Abc700

有任何想法吗?

4

2 回答 2

1

尝试这个:

'Return an integer which is greater than zero if the first string is "greater"
'than the second string, less than zero if the first string is "less" than the
'second string, and equal to zero if they are the same string.  It is assumed
'that both strings start with alpha characters and end with numeric characters.
Function CompareAlphaNumericStrings(First As String, Second As String) As Integer
    Dim arrFirstParts() As String
    arrFirstParts = SplitAlphaNumericString(First)
    Dim arrSecondParts() As String
    arrSecondParts = SplitAlphaNumericString(Second)

    Select Case StrComp(arrFirstParts(0), arrSecondParts(0), vbTextCompare)
        Case 0
            Dim intFirstNumeric As Integer
            intFirstNumeric = CInt(arrFirstParts(1))
            Dim intSecondNumeric As Integer
            intSecondNumeric = CInt(arrSecondParts(1))

            If (intFirstNumeric < intSecondNumeric) Then
                CompareAlphaNumericStrings = -1
            Else
                If (intFirstNumeric > intSecondNumeric) Then
                    CompareAlphaNumericStrings = 1
                Else 'they are equal.
                    CompareAlphaNumericStrings = 0
                End If
            End If
        Case Is < 0
            CompareAlphaNumericStrings = -1
        Case Is > 0
            CompareAlphaNumericStrings = 1
    End Select
End Function

'Split the provided string, which is presumably comprised of a set of alpha characters
'followed by a set of numeric characters, and return a two-element array
'containing first the alpha portion and second the numeric portion.
Function SplitAlphaNumericString(ToSplit) As String()
    Dim arrReturn(1) As String

    For i = 1 To Len(ToSplit)
        If (Not IsLetter(Mid(ToSplit, i, 1))) Then
            If (i > 1) Then arrReturn(0) = Left(ToSplit, i - 1) Else arrReturn(0) = "" 'If there is any alpha portion at all, grab it, otherwise empty string.
            arrReturn(1) = Mid(ToSplit, i) 'The rest should be numeric.
            Exit For
        End If
    Next

    SplitAlphaNumericString = arrReturn
End Function

'Return true if the provided string is a single character and that character is a letter (A - Z or a - z).
Function IsLetter(TestChar As String) As Boolean
    If Len(TestChar) = 1 Then
        If (TestChar >= "A" And TestChar <= "Z") Or _
            (TestChar >= "a" And TestChar <= "z") Then
            IsLetter = True
        Else
            IsLetter = False
        End If
    Else
        IsLetter = False
    End If
End Function

您可以在任何您喜欢的排序算法中使用它,例如冒泡排序:

Function SortValues()
    For i = 1 To 5
        For j = 1 To 4
            Dim strFirst As String
            Dim strSecond As String

            strFirst = Sheets(1).Cells(j, 1).Value
            strSecond = Sheets(1).Cells(j + 1, 1).Value

            'If the first belongs AFTER the second, swap them.
            If (CompareAlphaNumericStrings(strFirst, strSecond) > 0) Then
                Sheets(1).Cells(j, 1).Value = strSecond
                Sheets(1).Cells(j + 1, 1).Value = strFirst
            End If
        Next
    Next
End Function
于 2013-10-24T15:32:03.383 回答
0

首先输入以下两个UDF:

Public Function TextPart(sIn As String) As String
    Dim L As Long, LL As Long
    Dim sCh As String
    TextPart = ""
    For L = 1 To Len(sIn)
        sCh = Mid(sIn, L, 1)
        If sCh Like "[a-zA-Z]" Then
            TextPart = TextPart & sCh
        End If
    Next L
End Function

Public Function NumberPart(sIn As String) As Long
    Dim L As Long, LL As Long
    Dim sCh As String, temp As String
    For L = 1 To Len(sIn)
        sCh = Mid(sIn, L, 1)
        If sCh Like "[a-zA-Z]" Then
        Else
            temp = temp & sCh
        End If
    Next L
    NumberPart = CLng(temp)
End Function

然后在 B1 中输入:

=textpart(A1) 并复制下来

然后在 C1 中输入:

=numberpart(A1) 并复制下来

然后先按 B 再按 C 对列 A、B、C 进行排序。

你应该看到:

在此处输入图像描述

于 2013-10-24T15:30:12.070 回答