带有""的VBA文本到列分隔符

时间:2017-02-17 08:40:59

标签: excel vba excel-vba

我会很快。

我有一个变量' strLine'带文字:

确切的字符串将如下所示:

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

所以,我的案例中的分隔符是:"

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

预期细胞结果:

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

由于

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

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

答案 2 :(得分: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