可以使用Excel VBA中的split函数将单元格内的数据切分为数组,再使用bubble sort排序算法来实现,具体代码如下:
Sub SortByCriteria() Dim arrData() As String Dim lastRow As Long Dim i As Long, j As Long Dim temp As String Dim flag As Boolean
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow ' 循环每一行数据
arrData() = Split(Cells(i, "A"), ",") ' 将单元格内的数据切分为数组
flag = True
While flag
flag = False
For j = 0 To UBound(arrData()) - 1 ' 使用bubble sort算法进行排序
If arrData(j) > arrData(j + 1) Then
temp = arrData(j)
arrData(j) = arrData(j + 1)
arrData(j + 1) = temp
flag = True
End If
Next j
Wend
Cells(i, "B") = Join(arrData(), ",") ' 将排序后的数组重新合并为字符串,并写回单元格
Next i
End Sub