Sub Ham_StrReverse()
  Dim sReverse As String
    sReverse = StrReverse("1234567")
    MsgBox sReverse
End SubFunction Dao_Chuoi(ByVal Text As String) As String
Dim S As String
Dim p1, p2
Text = Trim(Text)
If Right(Text, 1) <> " " Then Text = Text & " "
S = ""
p1 = 0
p2 = 1
Do While p2 > 0
    p2 = InStr(p1 + 1, Text, " ")
    If p2 > 0 Then
        If S = "" Then
            S = Mid(Text, p1 + 1, p2 - p1 - 1) & S
        Else
            S = Mid(Text, p1 + 1, p2 - p1) & S
        End If
       p1 = p2
    End If
Loop
Dao_Chuoi = S
End FunctionPublic Function daochuoi_2(strText As String)
Dim strText2() As String
Dim strText3 As String
Dim i
strText = Trim(strText)
strText2 = Split(strText, Space(1))
For i = UBound(strText2) To 0 Step -1
    strText3 = Trim(strText3 & Space(1) & strText2(i))
Next i
daochuoi_2 = strText3
End FunctionCách đảo chuỗi của bạn cũng giống như cách đảo họ tên. Ví dụ "Phạm Duy Long" đảo thành "Long Duy Phạm", thường dùng cách này để sắp xếp họ tên theo ABC vì sắp theo tên.chibi đã viết:Chào các bạn, cho mình xin hàm đảo chuỗi, ví dụ
dao_chuoi("cong hoa xa hoi")="hoi xa hoa cong"
Function Dao_Chuoi(ByVal Text As String) As String
On Error GoTo RaiseErr
Dim S As String, tmpText As String
Dim p1, p2, nLen, nSpace1, nSpace2
Dao_Chuoi = Text
tmpText = Trim(Text)
If tmpText = "" Then Exit Function
nSpace1 = 0
GetSpace1:
    If Mid(Text, nSpace1 + 1, 1) = " " Then
        nSpace1 = nSpace1 + 1
        GoTo GetSpace1
    End If
    
nLen = Len(Text)
nSpace2 = 0
GetSpace2:
    If Mid(Text, nLen - nSpace2, 1) = " " Then
        nSpace2 = nSpace2 + 1
        GoTo GetSpace2
    End If
    
tmpText = tmpText & " "
S = ""
p1 = 0
p2 = 1
Do While p2 > 0
    p2 = InStr(p1 + 1, tmpText, " ")
    If p2 > 0 Then
        If S = "" Then
            S = Mid(tmpText, p1 + 1, p2 - p1 - 1) & S
        Else
            S = Mid(tmpText, p1 + 1, p2 - p1) & S
        End If
       p1 = p2
    End If
Loop
Dao_Chuoi = Space(nSpace2) & S & Space(nSpace1)
Exit Function
RaiseErr:
'Dao_Chuoi="Error!"
End FunctionHi, Anh!....
Như vậy cả hai hàm này đã xóa các dấu trống bên trong chuỗi chỉ để ngăn cách từ bởi một dấu trống. Là một chuỗi, sau khi đảo thì vẫn phải đảm bảo số ký tự trong một chuỗi khôing đổi (như ví dụ trên là 44).
.....
Option Explicit
Public Function daochuoi_2(strText As String)
Dim strText2() As String
Dim strText3 As String
Dim i
strText = strText
strText2 = Split(strText, Space(1))
For i = UBound(strText2) To 0 Step -1
    strText3 = strText3 & Space(1) & strText2(i)
Next i
daochuoi_2 = Mid$(strText3, 2)
End Functionnvson đã viết:Hi, Anh!
Em chủ yếu làm vậy mà.
Nếu muốn như anh thì:
Mã:Option Explicit Public Function daochuoi_2(strText As String) Dim strText2() As String Dim strText3 As String Dim i strText = strText strText2 = Split(strText, Space(1)) For i = UBound(strText2) To 0 Step -1 strText3 = strText3 & Space(1) & strText2(i) Next i daochuoi_2 = Mid$(strText3, 2) End Function
