gpt4 book ai didi

excel:如何在单元格内排序?

转载 作者:行者123 更新时间:2023-12-02 23:47:51 25 4
gpt4 key购买 nike

我有一个用逗号分隔的字符串,是否可以使用 Excel 公式对单元格内的值进行排序?

最佳答案

这是一个解决方案(从 here 窃取的快速排序代码)。您只需将一个按钮连接到 SortVals 宏,然后只需单击该按钮,它就会对事件单元格中的逗号分隔值进行排序。

Option Explicit

Public Sub SortVals()
Dim i As Integer
Dim arr As Variant
arr = Split(ActiveCell.Text, ",")

' trim values so sort will work properly
For i = LBound(arr) To UBound(arr)
arr(i) = Trim(arr(i))
Next i

' sort
QuickSort arr, LBound(arr), UBound(arr)

' load sorted values back to cell
Dim comma As String
comma = ""
ActiveCell = ""
For i = LBound(arr) To UBound(arr)
ActiveCell = ActiveCell & comma & CStr(arr(i))
comma = ","
Next i
End Sub

Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)

Dim pivot As Variant
Dim tmpSwap As Variant
Dim tmpLow As Long
Dim tmpHi As Long

tmpLow = inLow
tmpHi = inHi

pivot = vArray((inLow + inHi) \ 2)

While (tmpLow <= tmpHi)

While (vArray(tmpLow) < pivot And tmpLow < inHi)
tmpLow = tmpLow + 1
Wend

While (pivot < vArray(tmpHi) And tmpHi > inLow)
tmpHi = tmpHi - 1
Wend

If (tmpLow <= tmpHi) Then
tmpSwap = vArray(tmpLow)
vArray(tmpLow) = vArray(tmpHi)
vArray(tmpHi) = tmpSwap
tmpLow = tmpLow + 1
tmpHi = tmpHi - 1
End If

Wend

If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi

End Sub

关于excel:如何在单元格内排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3399823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com