我會很快。用「」分隔符將VBA文本添加到列
我有一個文本變量 'strLine中':
確切的字符串看起來像:
"Text1","Text2","Text3","Text4","Text5"
所以,分隔在我的情況是:「
如何我可以提取文本並將其寫入列中。
期望在單元格中顯示結果:
A1=Text1
B1=Text2
C1=Text3
D1=Text4
E1=Text5
感謝
我會很快。用「」分隔符將VBA文本添加到列
我有一個文本變量 '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
如果'Str(i)'中包含引號,則可能需要'Mid'而不是'Replace' –
Fantastic,Thanks @ R3uk – Andy
您可以使用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
一些解釋會提高答案質量。它現在被標記爲低質量郵政。 – Starx
如何將'A1'切換到for循環中的下一個字符串的下一行? – Andy
你從哪裏閱讀這些字符串? – user3598756
你的分隔符是',''沒有 「'然後拆分後','你需要剪掉'。」'。 –