[英]VBA text to columns with “” delimiters
我會很快。
我有一個帶有文字的變量“ strLine”:
確切的字符串如下所示:
"Text1","Text2","Text3","Text4","Text5"
因此,在我的情況下,分隔符為:
如何提取文本並將其寫入列中。
單元格中的預期結果:
A1=Text1
B1=Text2
C1=Text3
D1=Text4
E1=Text5
謝謝
使用Split
函數,它將返回一個從0開始的數組(單元格中的ergo +1):
Sub test_Andy()
Dim StrLine As String
Dim Str() As String
StrLine = Range("A2").Value 'If your value is in A2
'If you input the value manually, not really practical with so much "
StrLine = Chr(34) & "Text1" & Chr(34) & "," & Chr(34) & "Text2" & Chr(34) & "," & Chr(34) & "Text3" & Chr(34) & "," & Chr(34) & "Text4" & Chr(34) & "," & Chr(34) & "Text5" & Chr(34)
Debug.Print StrLine '"Text1","Text2","Text3","Text4","Text5"
Str = Split(StrLine, ",")
Dim i As Integer
For i = LBound(Str) To UBound(Str)
Cells(1, i + 1) = Str(i)
Cells(2, i + 1) = Replace(Str(i), Chr(34), vbNullString)
Next i
End Sub
您可以使用Split
將文本拆分為一個數組,然后使用Mid
從部分的開頭和結尾刪除"
:
strText = """Text1"",""Text2"",""Text3"",""Text4"",""Text5"""
aSplit = Split(strText, ",")
For Each strCurrent in aSplit
MsgBox Mid(strCurrent, 2, Len(strCurrent) - 2)
Next
備注:您可能需要添加一些檢查,以確保在刪除它們之前在開頭和結尾都有"
。
編輯以模擬StrLine
循環:
Dim iRow As Long
irow = 1
For Each StrLine In StrLines '<--' assuming a loop through many StrLines
Str = Split(Replace(StrLine, """", ""), ",")
Cells(iRow, 1).Resize(, UBound(Str) - LBound(Str)).Value = Str
iRow = iRow + 1
Next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.