我正在尝试在Excel中的单元格中对一组数字进行排序,并且需要保持相同的格式。具体而言,在A列中,我有一组单元格,其中年份按降序显示。超过一年时,年份由逗号和空格分隔。例如:
A
1 2017, 2016, 2015, 2014, 2013, 2012
2 2015
3 2017, 2016, 2015
4 2017, 2016
我可以使用什么公式,以便单元格中的年份按升序排序,以便在B列中我将看到:
B
1 2012, 2013, 2014, 2015, 2016, 2017
2 2015
3 2015, 2016, 20175
4 2016, 2017
答案 0 :(得分:1)
如果您拥有Excel 2016和TEXTJOIN
功能,则可以使用此数组输入公式:
=TEXTJOIN(", ",TRUE,AGGREGATE(15,7,--TRIM(MID(SUBSTITUTE(A1,",",REPT(" ",99)),seq_99,99)),ROW(INDIRECT("1:"&LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1))))
seq_99
是一个命名公式,引用:
=IF(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))=1,1,(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))-1)*99)
要输入数组公式,请在按输入时按住 ctrl + shift 。
如果您正确执行此操作,您将在公式栏中看到Excel已在公式周围放置大括号{...}
。
答案 1 :(得分:0)
以下用户定义函数如何:
Public Function Zort(s As String) As String
Zort = ""
If s = "" Then Exit Function
If InStr(s, ",") = 0 Then
Zort = s
Exit Function
End If
brr = Split(Replace(s, " ", ""), ",")
Call aSort(brr)
Zort = Join(brr, ", ")
End Function
Public Sub aSort(ByRef InOut)
Dim I As Long, J As Long, Low As Long
Dim Hi As Long, Temp As Variant
Low = LBound(InOut)
Hi = UBound(InOut)
J = (Hi - Low + 1) \ 2
Do While J > 0
For I = Low To Hi - J
If InOut(I) > InOut(I + J) Then
Temp = InOut(I)
InOut(I) = InOut(I + J)
InOut(I + J) = Temp
End If
Next I
For I = Hi - J To Low Step -1
If InOut(I) > InOut(I + J) Then
Temp = InOut(I)
InOut(I) = InOut(I + J)
InOut(I + J) = Temp
End If
Next I
J = J \ 2
Loop
End Sub