颠倒句序 excel
Reverse sentence order excel
我只能找到反转单词的函数,例如你好 -> olleH。但是,我需要一个函数来将句子的顺序从后到前颠倒过来。一个例子可以是:“你好,我的名字是”->“是我的名字,你好”。
在VBA或预建函数中有什么建议吗?
谢谢
用Excel365,你可以试试:
B1
中的公式:
=LET(X,FILTERXML("<t><s>"&SUBSTITUTE(A1," ","</s><s>")&"</s></t>","//s"),TEXTJOIN(" ",,SORTBY(X,SEQUENCE(COUNTA(X)),-1)))
没有LET()
的可以尝试使用Excel 2019:
=TEXTJOIN(" ",,INDEX(FILTERXML("<t><s>"&SUBSTITUTE(A1," ","</s><s>")&"</s></t>","//s"),N(IF(1,LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+2-ROW(A1:INDEX(A:A,(LEN(A1)-LEN(SUBSTITUTE(A1," ","")))+1))))))
显然,这需要您进行 CSE 输入公式。
编辑 2022 年 3 月 3 日:
根据ms365的最新功能,我们可以试试:
=REDUCE("",TEXTSPLIT(A1," "),LAMBDA(a,b,TEXTJOIN(" ",,b,a)))
或者:
=TRIM(REDUCE("",TEXTSPLIT(A1," "),LAMBDA(a,b,b&" "&a)))
Great thanks, the best would be a function which I can control within other functions... – emilk 36 mins ago
这是一个替代方法,您可以在其中传递具有值的范围。
Option Explicit
Sub Sample()
MsgBox ReverseString(Range("A1"))
End Sub
Private Function ReverseString(rng As Range) As String
On Error GoTo Whoa
Dim MyAr As Variant, itm As Variant
MyAr = Split(rng.Value2, " ")
Dim arListCollection As Object
Set arListCollection = CreateObject("System.Collections.ArrayList")
With arListCollection
For Each itm In MyAr: .Add itm: Next itm
.Reverse
MyAr = .Toarray
End With
ReverseString = Join(MyAr, " ")
LetsContinue:
Exit Function
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Function
如果你想传递一个字符串给函数,例如
Sub Sample()
MsgBox ReverseString("Hello my name is")
End Sub
那么函数就变成了
Private Function ReverseString(s As String) As String
On Error GoTo Whoa
Dim MyAr As Variant, itm As Variant
MyAr = Split(s, " ")
Dim arListCollection As Object
Set arListCollection = CreateObject("System.Collections.ArrayList")
With arListCollection
For Each itm In MyAr: .Add itm: Next itm
.Reverse
MyAr = .Toarray
End With
ReverseString = Join(MyAr, " ")
LetsContinue:
Exit Function
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Function
请尝试下一个简单的功能:
Function revWordsInSentance(x As String) As String
Dim arr, arrRev, i As Long
arr = Split(x, " "): ReDim arrRev(UBound(arr))
For i = 0 To UBound(arr): arrRev(i) = arr(UBound(arr) - i): Next i
revWordsInSentance = Join(arrRev, " ")
End Function
可以通过以下方式进行测试:
Sub testRevWordsInSent()
MsgBox revWordsInSentance("Hello my name is")
End Sub
版本 365
只是为了好玩,通过 Sequence()
函数 (版本 365):
的替代方案
Function getReverse2(sentence As String) As String
If sentence = vbNullString Then Exit Function
'a) split string into tokens
Dim tokens: tokens = Split(sentence)
'b) build decreasing series
Dim newOrder: newOrder = Application.Sequence(1, UBound(tokens) + 1, UBound(tokens) + 1, -1)
'c) rearrange tokens in descending column order
getReverse2 = Join(Application.Index(tokens, 0, newOrder))
End Function
编辑 #1 向后兼容 // 截至 2021 年 1 月 14 日
不像上面的原始方法那么短,但回复@FaneDuru 的评论以提供向后兼容的方法:
Function getReverse(sentence As String) As String
If sentence = vbNullString Then Exit Function
'a) split string into tokens
Dim tokens: tokens = Split(sentence)
'b) build decreasing series
Dim neworder: neworder = getNewOrder(tokens) ' << added function call
'c) rearrange tokens in descending column order
getReverse = Join(Application.Index(tokens, 0, neworder))
End Function
帮助功能getNewOrder()
Function getNewOrder(arr, Optional ByVal Is365 = False)
'' //version 365:
' getNewOrder = Application.Sequence(1, UBound(tokens) + 1, UBound(tokens) + 1, -1)
'' //prior versions '
Dim cols As Long: cols = UBound(arr) + 1: ReDim tmp(1 To cols)
Dim col As Long: For col = 1 To cols: tmp(col) = cols - col + 1: Next col
getNewOrder = tmp
End Function
►编辑 #2 向后兼容 - 改进 // 截至 2021-11-12
我没有循环模仿反向 Sequence
(vers. MS 365) 我通过基于列号的评估找到了一个解决方法:
newOrder = Evaluate(i & "-Column(A:" & Split(Cells(1, i).Address, "$")(1) & ")+1")
其中Split(Cells(1, i).Address, "$")(1)
只是获取起始元素编号i
的列名。
Function getReverse2(sentence As String) As String
If sentence = vbNullString Then Exit Function
'a) split string into tokens
Dim tokens: tokens = Split(sentence)
Dim i As Long: i = UBound(tokens) + 1 ' number of splitted elements
'b) build decreasing series
'Dim newOrder: newOrder = Application.Sequence(1, UBound(tokens) + 1, UBound(tokens) + 1, -1)
Dim newOrder
newOrder = Evaluate(i & "-Column(A:" & Split(Cells(1, i).Address, "$")(1) & ")+1")
'c) rearrange tokens in descending column order
getReverse2 = Join(Application.Index(tokens, 0, newOrder))
End Function
我只能找到反转单词的函数,例如你好 -> olleH。但是,我需要一个函数来将句子的顺序从后到前颠倒过来。一个例子可以是:“你好,我的名字是”->“是我的名字,你好”。
在VBA或预建函数中有什么建议吗?
谢谢
用Excel365,你可以试试:
B1
中的公式:
=LET(X,FILTERXML("<t><s>"&SUBSTITUTE(A1," ","</s><s>")&"</s></t>","//s"),TEXTJOIN(" ",,SORTBY(X,SEQUENCE(COUNTA(X)),-1)))
没有LET()
的可以尝试使用Excel 2019:
=TEXTJOIN(" ",,INDEX(FILTERXML("<t><s>"&SUBSTITUTE(A1," ","</s><s>")&"</s></t>","//s"),N(IF(1,LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+2-ROW(A1:INDEX(A:A,(LEN(A1)-LEN(SUBSTITUTE(A1," ","")))+1))))))
显然,这需要您进行 CSE 输入公式。
编辑 2022 年 3 月 3 日:
根据ms365的最新功能,我们可以试试:
=REDUCE("",TEXTSPLIT(A1," "),LAMBDA(a,b,TEXTJOIN(" ",,b,a)))
或者:
=TRIM(REDUCE("",TEXTSPLIT(A1," "),LAMBDA(a,b,b&" "&a)))
Great thanks, the best would be a function which I can control within other functions... – emilk 36 mins ago
这是一个替代方法,您可以在其中传递具有值的范围。
Option Explicit
Sub Sample()
MsgBox ReverseString(Range("A1"))
End Sub
Private Function ReverseString(rng As Range) As String
On Error GoTo Whoa
Dim MyAr As Variant, itm As Variant
MyAr = Split(rng.Value2, " ")
Dim arListCollection As Object
Set arListCollection = CreateObject("System.Collections.ArrayList")
With arListCollection
For Each itm In MyAr: .Add itm: Next itm
.Reverse
MyAr = .Toarray
End With
ReverseString = Join(MyAr, " ")
LetsContinue:
Exit Function
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Function
如果你想传递一个字符串给函数,例如
Sub Sample()
MsgBox ReverseString("Hello my name is")
End Sub
那么函数就变成了
Private Function ReverseString(s As String) As String
On Error GoTo Whoa
Dim MyAr As Variant, itm As Variant
MyAr = Split(s, " ")
Dim arListCollection As Object
Set arListCollection = CreateObject("System.Collections.ArrayList")
With arListCollection
For Each itm In MyAr: .Add itm: Next itm
.Reverse
MyAr = .Toarray
End With
ReverseString = Join(MyAr, " ")
LetsContinue:
Exit Function
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Function
请尝试下一个简单的功能:
Function revWordsInSentance(x As String) As String
Dim arr, arrRev, i As Long
arr = Split(x, " "): ReDim arrRev(UBound(arr))
For i = 0 To UBound(arr): arrRev(i) = arr(UBound(arr) - i): Next i
revWordsInSentance = Join(arrRev, " ")
End Function
可以通过以下方式进行测试:
Sub testRevWordsInSent()
MsgBox revWordsInSentance("Hello my name is")
End Sub
版本 365
只是为了好玩,通过 Sequence()
函数 (版本 365):
Function getReverse2(sentence As String) As String
If sentence = vbNullString Then Exit Function
'a) split string into tokens
Dim tokens: tokens = Split(sentence)
'b) build decreasing series
Dim newOrder: newOrder = Application.Sequence(1, UBound(tokens) + 1, UBound(tokens) + 1, -1)
'c) rearrange tokens in descending column order
getReverse2 = Join(Application.Index(tokens, 0, newOrder))
End Function
编辑 #1 向后兼容 // 截至 2021 年 1 月 14 日
不像上面的原始方法那么短,但回复@FaneDuru 的评论以提供向后兼容的方法:
Function getReverse(sentence As String) As String
If sentence = vbNullString Then Exit Function
'a) split string into tokens
Dim tokens: tokens = Split(sentence)
'b) build decreasing series
Dim neworder: neworder = getNewOrder(tokens) ' << added function call
'c) rearrange tokens in descending column order
getReverse = Join(Application.Index(tokens, 0, neworder))
End Function
帮助功能getNewOrder()
Function getNewOrder(arr, Optional ByVal Is365 = False)
'' //version 365:
' getNewOrder = Application.Sequence(1, UBound(tokens) + 1, UBound(tokens) + 1, -1)
'' //prior versions '
Dim cols As Long: cols = UBound(arr) + 1: ReDim tmp(1 To cols)
Dim col As Long: For col = 1 To cols: tmp(col) = cols - col + 1: Next col
getNewOrder = tmp
End Function
►编辑 #2 向后兼容 - 改进 // 截至 2021-11-12
我没有循环模仿反向 Sequence
(vers. MS 365) 我通过基于列号的评估找到了一个解决方法:
newOrder = Evaluate(i & "-Column(A:" & Split(Cells(1, i).Address, "$")(1) & ")+1")
其中Split(Cells(1, i).Address, "$")(1)
只是获取起始元素编号i
的列名。
Function getReverse2(sentence As String) As String
If sentence = vbNullString Then Exit Function
'a) split string into tokens
Dim tokens: tokens = Split(sentence)
Dim i As Long: i = UBound(tokens) + 1 ' number of splitted elements
'b) build decreasing series
'Dim newOrder: newOrder = Application.Sequence(1, UBound(tokens) + 1, UBound(tokens) + 1, -1)
Dim newOrder
newOrder = Evaluate(i & "-Column(A:" & Split(Cells(1, i).Address, "$")(1) & ")+1")
'c) rearrange tokens in descending column order
getReverse2 = Join(Application.Index(tokens, 0, newOrder))
End Function