0

我在 Excel 中有一个列,格式为:

A01G45B45D12

我需要一种像这样格式化它的方法,即将字符串分成三个字符的组,按字母顺序对组进行排序,然后用 + 号将它们连接在一起:

A01+B45+D12+G45

我想知道这是否可以使用 Excel 中的内置公式,或者如果我必须使用 VBA 或其他东西来做到这一点,如果有一种简单的方法可以从 Excel 中使用它,我已经在 C# 中有代码。我以前没有为 Excel 编写过插件。

编辑补充:上面只是一个例子,字符串可以是“任意长度”,但它总是能被三整除,而且顺序是随机的,所以我不能事先假设任何关于顺序的事情。

4

1 回答 1

0
Sub ArraySort()

Dim strStarter As String
Dim strFinish As String
Dim intHowMany As Integer
Dim intStartSlice As Integer

    strStarter = ActiveCell.Offset(0, -1).Value 'Pulls value from cell to the left

    intHowMany = Int(Len(strStarter) / 3)
    ReDim arrSlices(1 To intHowMany) As String


    intStartSlice = 1

    For x = 1 To intHowMany

        arrSlices(x) = Mid(strStarter, intStartSlice, 3)

        intStartSlice = intStartSlice + 3

    Next x

    Call BubbleSort(arrSlices)

    For x = 1 To intHowMany

        strFinish = strFinish + arrSlices(x) & "+"

    Next x


strFinish = Left(strFinish, Len(strFinish) - 1)
ActiveCell.Value = strFinish 'Puts result into activecell

End Sub


Sub BubbleSort(list() As String)
'Taken from power programming with VBA
'It’s a sorting procedure for 1-dimensional arrays named List
'The procedure takes each array element, if it is greater than the next element, the two elements swap positions.
'The evaluation is repeated for every pair of items (that is n-1 times)

    Dim First As Integer, Last As Long
    Dim i As Long, j As Long
    Dim temp As String
    First = LBound(list)
    Last = UBound(list)

    For i = First To Last - 1
        For j = i + 1 To Last
            If list(i) > list(j) Then
                temp = list(j)
                list(j) = list(i)
                list(i) = temp
            End If
        Next j
    Next i

End Sub
于 2013-04-30T11:55:08.463 回答