逆句序excel

如何解决逆句序excel

我只能找到反转单词的函数,例如你好 -> olleH。但是,我需要一个将句子从最后到第一的顺序颠倒的函数一个例子可以是:“你好,我的名字是”->“我的名字是你好”。

对 VBA 或预建函数有什么建议吗?

谢谢

解决方法

使用 Excel 365,您可以尝试:

enter image description here

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,N(IF(1,LEN(A1)-LEN(SUBSTITUTE(A1,""))+2-ROW(A1:INDEX(A:A,(LEN(A1)-LEN(SUBSTITUTE(A1,"")))+1))))))

显然,这需要您通过 CSE 输入公式。

,

非常感谢,最好的功能是我可以在其他功能中控制... – emilk 36 分钟前

这是一种替代方法,您可以在其中传递具有值的范围。

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," ")
LetsContinue:
    Exit Function
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Function

有趣的阅读: Quick Guide to the VBA ArrayList

,

请尝试下一个简单的功能:

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,-1)
    'c) rearrange tokens in descending column order
    getReverse2 = Join(Application.Index(tokens,newOrder))
End Function


编辑 #1

不像上面的原始方法那么短,而是回复@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,neworder))
End Function

帮助功能getNewOrder()

Function getNewOrder(arr,Optional ByVal Is365 = False)
    '' //version 365:
    '    getNewOrder = Application.Sequence(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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?