2017-02-17 88 views
1

我会很快。用“”分隔符将VBA文本添加到列

我有一个文本变量 'strLine中':

确切的字符串看起来像:

"Text1","Text2","Text3","Text4","Text5"

所以,分隔在我的情况是:“

如何我可以提取文本并将其写入列中。

期望在单元格中显示结果:

A1=Text1 
B1=Text2 
C1=Text3 
D1=Text4 
E1=Text5 

感谢

+2

你的分隔符是',''没有 “'然后拆分后','你需要剪掉'。”'。 –

回答

2

使用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 
+2

如果'Str(i)'中包含引号,则可能需要'Mid'而不是'Replace' –

+0

Fantastic,Thanks @ R3uk – Andy

1

您可以使用Split将文本分成数组,然后使用Mid除去从一开始和部件的端部的"

strText = """Text1"",""Text2"",""Text3"",""Text4"",""Text5""" 
aSplit = Split(strText, ",") 
For Each strCurrent in aSplit 
    MsgBox Mid(strCurrent, 2, Len(strCurrent) - 2) 
Next 

备注:您可能需要添加一些检查以确保在删除它们之前在开始和结束时有一个"

1

编辑模拟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 
+0

一些解释会提高答案质量。它现在被标记为低质量邮政。 – Starx

+0

如何将'A1'切换到for循环中的下一个字符串的下一行? – Andy

+0

你从哪里阅读这些字符串? – user3598756