[英]VBA Excel split the string apart based on the last comma
我想在地址和郵政編碼之間拆分 Excel 中的字符串。 我想單獨保留郵政編碼。
通過選擇選項 - 數據 - 文本到列 - 分隔 - 逗號分隔 - 整個字符串除以 4 部分,因為出現 3 個逗號。
1 - 21 Willow Court, 1192 Christchurch Road, 伯恩茅斯, BH7 6EG
我發現,它可以在 VBA Excel 中完成。
下面有幾種方法:
https://www.thespreadsheetguru.com/the-code-vault/2014/2/28/remove-last-character-from-string
如何在 excel 中使用 VBA 刪除“*”或“-”字符后的文本?
我准備了 VBA 代碼,如下所示:
Sub Textremove()
Dim c As Variant
For Each c In Range("D1:D100")
c.Value = Left(c.Value, InStr(c.Value, ",") - 1)
Next c
End Sub
我只收到:
1 - 21 柳苑
和錯誤無效的過程調用或參數,調試以下行:
c.Value = Left(c.Value, InStr(c.Value, ",") - 1)
因此,故障發生在第一個逗號而不是最后一個逗號之后。
我找到了有關此錯誤的答案:
當我的代碼看起來像這樣時:
Sub Textremove()
Dim c As Variant
For Each c In Range("D1:D100")
If InStr(c.Value, ",") > 0 Then
c.Value = Left(c.Value, InStr(c.Value, ",") - 1)
End If
Next c
End Sub
然后錯誤不再發生,但我仍然得到這些東西,直到第一個逗號而不是最后一個逗號。
當我稍微更改代碼時:
Sub Textremove()
Dim c As Variant
For Each c In Range("D1:D100")
If InStr(c.Value, ",") > 0 Then
c.Value = Right(c.Value, InStr(c.Value, ","))
End If
Next c
End Sub
我從右邊得到 2 個句子
伯恩茅斯,BH7 6EG
它們不是固定的,會根據字符串的總長度而變化。
我怎樣才能接收到最后一個逗號而不是第一個逗號的字符串? 如何分別拆分地址和郵政編碼之間的整個字符串?
一個很好的例子在這里:
https://trumpexcel.com/vba-split-function/
Sub CommaSeparator()
Dim TextStrng As String
Dim Result() As String
Dim DisplayText As String
Dim i As Long
TextStrng = Sheets("Final").Range("D1")
Result = Split(TextStrng, ",", 1)
For i = LBound(Result()) To UBound(Result())
DisplayText = DisplayText & Result(i) & vbNewLine
Next i
MsgBox DisplayText
End Sub
誠然,它拆分了整個地址,但仍從第一個逗號開始計算。
就我而言,這是可行的。 我剛剛添加了 UBound(Result())-1。
Sub CommaSeparator()
Dim TextStrng As String
Dim Result() As String
Dim DisplayText As String
Dim i As Long
TextStrng = Sheets("Final").Range("D1")
Result = Split(TextStrng, ",")
For i = LBound(Result()) To UBound(Result()) - 1
DisplayText = DisplayText & Result(i) & vbNewLine
Next i
MsgBox DisplayText
End Sub
如果您需要 VBA,可以使用:
Sub Test()
Dim str As String
Dim arr As Variant
str = "1 - 21 Willow Court, 1192 Christchurch Road, Bournemouth, BH7 6EG"
arr = Split(StrReverse(Replace(StrReverse(str), ",", "|", , 1)), "|")
End Sub
我通過StrReverse()
反轉了整個字符串,然后使用Replace()
僅用管道符號替換第一個逗號(注意使用Count
參數),將字符串反轉並使用Split()
。 這將返回:
另一種方法是使用工作表函數REPLACE()
而不是 VBA function 不方便地稱為相同。
Sub Test()
Dim str As String: str = "1 - 21 Willow Court, 1192 Christchurch Road, Bournemouth, BH7 6EG"
Dim arr As Variant
arr = Split(Application.Replace(str, InStrRev(str, ","), 1, "|"), "|")
End Sub
現在的主要區別在於Application.Replace
確實需要一個參數來開始替換,而無需剪切前面的文本。 我們可以使用InstrRev()
找到我們的起始 position 。
兩個選項都返回:
只是為了好玩,我會加入一個正則表達式解決方案:
Sub Test()
Dim str As String: str = "1 - 21 Willow Court, 1192 Christchurch Road, Bournemouth, BH7 6EG"
Dim arr As Variant
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "^.*(?=,)|[^,]+$"
Set arr = .Execute(str)
End With
End Sub
這將返回一個“MatchCollectionObject”,您可以在其中通過以下方式調用您的結果: arr(0)
和arr(1)
。 對模式的一點解釋:
^
- 開始字符串錨。.*
- 除了換行符之外的任何內容的貪婪匹配:(?=,)
- 逗號的正向前瞻。|
- 或匹配:[^,]$
- 除了逗號之外的任何內容,直到結束字符串錨點。查看在線演示
使用Split
返回的數組來重建你喜歡的字符串,例如:
Sub DoSplit()
s = "1 - 21 Willow Court, 1192 Christchurch Road, Bournemouth, BH7 6EG"
a = Split(s, ",")
finalString = a(0) & a(1) & a(2) & ", " & a(3)
MsgBox finalString
End Sub
我以不同的兩步方式對此進行了排序。
首先,我使用此處的公式拆分了整個地址:
Sub Split()
Dim MyArray() As String
Dim Ws As Worksheet
Dim lRow As Long, i As Long, j As Long, c As Long
'~~> Change this to the relevant sheet name
Set Ws = ThisWorkbook.Sheets("Final")
With Ws
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
For i = 1 To lRow
If InStr(1, .Range("E" & i).Value, ",", vbTextCompare) Then
MyArray = Split(.Range("E" & i).Value, ",")
c = 1
For j = 0 To UBound(MyArray)
.Cells(i, c).Value = MyArray(j)
c = c + 1
Next j
End If
Next i
End With
End Sub
接下來,我使用這個提示合並了我需要的東西:
Sub Merge()
Dim LastRow As Long
Dim Ws As Worksheet
Set Ws = Sheets("Final")
LastRow = Ws.Range("A" & Ws.Rows.Count).End(xlUp).Row
'~~> If your range doesn't have a header
Ws.Range("H1:H" & LastRow).Formula = "=A1&B1&C1"
'~~> If it does then
Ws.Range("H2:H" & LastRow).Formula = "=A2&B2&C2"
End Sub
最后,我收到了:
1 - 10 Haviland Court 104 Haviland Road 伯恩茅斯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.