幾種用EXCEL實現字串的"顛倒"的方法
Reverse text string with formula in Excel
Amazing! Using Tabs in Excel like Firefox, Chrome, Internet Explore 10!
In Excel, you can use a formula to reverse text string. For example, there are some text string as shown as below in Excel:
Step 1. Type this formula =IF(LEN(A1)<1,"",MID(A1,LEN(A1),1))&IF(LEN(A1)<2,"",MID(A1,LEN(A1)-1,1))&IF(LEN(A1)<3,"",MID(A1,LEN(A1)-2,1))&IF(LEN(A1)<4,"",MID(A1,LEN(A1)-3,1))&IF(LEN(A1)<5,"",MID(A1,LEN(A1)-4,1))
Step 2. Copy this formula to other cells by dragging the bottom right corner of the Cell B1. See screenshot:
Note:
1. In this case, each of text string has five characters, so here the formula has five sections. If the text string has six characters, you need to add &IF(LEN(A1)<6,"",MID(A1,LEN(A1)-5,1))
2. If each text string has a different number of characters in the column, you cannot drag the bottom right corner to copy the formula, you need to type the different formulas one by one.
Reverse text string with VBA
Amazing! Using Tabs in Excel like Firefox, Chrome, Internet Explore 10!
Supposing you have a range of text strings which you want to reverse, such as “add leading zeros in Excel” to “lecxE ni sorez gnidael dda”. You can reverse the text with following steps:
1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following macro in the Modulewindow.
Sub ReverseText()
'Updateby20131128
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
xValue = Rng.Value
xLen = VBA.Len(xValue)
xOut = ""
For i = 1 To xLen
getChar = VBA.Right(xValue, 1)
xValue = VBA.Left(xValue, xLen - i)
xOut = xOut & getChar
Next
Rng.Value = xOut
Next
End Sub
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Sub ReverseText()
'Updateby20131128
Dim Rng As Range
Dim WorkRng As Range
On Error Resume
Next
xTitleId
= "KutoolsforExcel"
Set WorkRng
= Application.Selection
Set WorkRng
= Application.InputBox( "Range" ,
xTitleId, WorkRng.Address, Type:=8)
For Each Rng
In WorkRng
xValue
= Rng.Value
xLen
= VBA.Len(xValue)
xOut
= ""
For i
= 1 To xLen
getChar
= VBA.Right(xValue, 1)
xValue
= VBA.Left(xValue, xLen - i)
xOut
= xOut & getChar
Next
Rng.Value
= xOut
Next
End Sub
|
3. Then press F5, a dialog is displayed on the screen, and you need select a range to work with. See screenshot:
4. And then press OK, and all the text strings have been reversed. See screenshot:
Reverse
words separated by interval symbol with VBA
If you have a list of cell words which are separated by commas as this “teacher, doctor, student, worker, driver”, and you want to reverse the words order like this “drive, worker, student, doctor, teacher”. You can also use follow VBA to solve it.
1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following macro in the Module window.
Sub ReverseWord()
'Updateby20131128
Dim Rng As Range
Dim WorkRng As Range
Dim Sigh As String
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Sigh = Application.InputBox("Symbol interval", xTitleId, ",", Type:=2)
For Each Rng In WorkRng
strList = VBA.Split(Rng.Value, Sigh)
xOut = ""
For i = UBound(strList) To 0 Step -1
xOut = xOut & strList(i) & Sigh
Next
Rng.Value = xOut
Next
End Sub
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Sub ReverseWord()
'Updateby20131128
Dim Rng As Range
Dim WorkRng As Range
Dim Sigh As String
On Error Resume
Next
xTitleId
= "KutoolsforExcel"
Set WorkRng
= Application.Selection
Set WorkRng
= Application.InputBox( "Range" ,
xTitleId, WorkRng.Address, Type:=8)
Sigh
= Application.InputBox( "Symbol
interval" ,
xTitleId, "," ,
Type:=2)
For Each Rng
In WorkRng
strList
= VBA.Split(Rng.Value, Sigh)
xOut
= ""
For i
= UBound(strList) To 0 Step -1
xOut
= xOut & strList(i) & Sigh
Next
Rng.Value
= xOut
Next
|