[英]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.