拆分分隔符必须设置为“,”,但返回的值可能包含逗号

我试图运行一个程序,该程序应该从电子表格中提取数据,将数据拆分成块,然后根据它的“值”变体将其导入到我的表中。 数据的格式如下所示:

“HL”,“Hecla Mining Company Mining Stock”,“NSM”,12.52,8.69,14.07,6.18

用于分割行,定义值并将它们分配给列的代码当前被编写如下:

Dim Resp As String: Resp = Http.ResponseText Dim Lines As Variant: Lines = Split(Resp, vbLf) Dim sLine As String Dim Values As Variant For i = 0 To UBound(Lines) sLine = Lines(i) If InStr(sLine, ",") > 0 Then Values = Split(sLine, ",") W.Cells(i + 2, 2).Value = Replace(Values(1), Chr(34), "") W.Cells(i + 2, 5).Value = Replace(Values(2), Chr(34), "") W.Cells(i + 2, 6).Value = Values(3) W.Cells(i + 2, 7).Value = Values(4) W.Cells(i + 2, 8).Value = Values(5) W.Cells(i + 2, 9).Value = Values(6) W.Cells(i + 2, 10).Value = Values(7) W.Cells(i + 2, 11).Value = Values(8) W.Cells(i + 2, 13).Value = Values(9) End If 

发生这个问题的一些行会返回一个包含逗号的名称,例如:

“CDE”,“Coeur Mining,Inc。”,“NSM”,7.59,16.25,9.52,7.01

这导致值(2)=“Coeur挖掘”和价值(3)=“公司” 而不是价值观(2)=“Coeur矿业公司” 和值(3)=“NSM”

我已经尝试更新以下代码:

 Dim Resp As String: Resp = Http.ResponseText Dim Lines As Variant: Lines = Split(Resp, vbLf) Dim sLine As String Dim Values As Variant For i = 0 To UBound(Lines) sLine = Lines(i) If InStr(sLine, ",") > 0 Then ***If InStr(sLine, ",Inc.") Then sLine = Replace(sLine, ",inc.", "") End If*** Values = Split(sLine, ",") W.Cells(i + 2, 2).Value = Replace(Values(1), Chr(34), "") W.Cells(i + 2, 5).Value = Replace(Values(2), Chr(34), "") W.Cells(i + 2, 6).Value = Values(3) W.Cells(i + 2, 7).Value = Values(4) W.Cells(i + 2, 8).Value = Values(5) W.Cells(i + 2, 9).Value = Values(6) W.Cells(i + 2, 10).Value = Values(7) W.Cells(i + 2, 11).Value = Values(8) W.Cells(i + 2, 13).Value = Values(9) End If 

但是,即使嵌套的If语句正在寻找“,Inc”,它也不会起作用。 在sLinestring内。

有没有格式问题,我没有得到? 我试图使用正则expression式函数,但我是非常新的Excel / VBA,无法弄清楚如何正确格式化。

build议的正则expression式代码如下:

 Public Function splitLine(line As String) As String() Dim regex As Object Set regex = CreateObject("vbscript.regexp") regex.IgnoreCase = True regex.Global = True regex.Pattern = ",(?=([^" & Chr(34) & "]" & Chr(34) & "[^" & Chr(34) & "]" & Chr(34) & ")(?![^" & Chr(34) & "]" & Chr(34) & "))" splitLine = Split(regex.Replace(line, ";"), ";") End Function Values = splitLine(sLine) 

任何帮助将不胜感激,更多的信息或实际的Excel文件的副本可应要求提供。

看起来你将不得不通过一个模仿Text-to-Columns的引用文本参数的'helper'函数来处理string。

虽然不雅(可能很容易改善),这适用于您的示例。

 Option Explicit Sub test() Dim str As String, var As Variant str = """CDE"",""Coeur Mining, Inc."",""NSM"",7.59,16.25,9.52,7.01" With Worksheets("Sheet1") Debug.Print str str = cleanQuotedCommas(str) var = Split(str, Chr(44)) With .Cells(2, "B").Resize(1, UBound(var) + 1) .Value = var .Replace what:=ChrW(8203), replacement:=Chr(44), lookat:=xlPart .Replace what:=Chr(34), replacement:=vbNullString, lookat:=xlPart .Value = .Value2 End With End With End Sub Function cleanQuotedCommas(str As String) As String Dim i As Long, j As Long, k As Long i = InStr(1, str, Chr(34), vbBinaryCompare) Do While CBool(i) j = InStr(i + 1, str, Chr(34), vbBinaryCompare) k = InStr(i + 1, str, Chr(44), vbBinaryCompare) If k > i And k < j Then str = Replace(str, Chr(44), ChrW(8203), i, 1, vbBinaryCompare) End If Debug.Print str i = InStr(j + 1, str, Chr(34), vbBinaryCompare) Loop cleanQuotedCommas = str End Function 

在这里输入图像说明

注意双精度和左alignment的正确数字。

这是一个基于正则expression式的SplitLine函数,它将返回一个string数组。 它将从包含它的条目中排除周围的引号,并且不会在“包含”逗号分割:

 Option Explicit Public Function splitLine(line As String) As String() Dim regex As Object, matchcol As Object, match As Object Dim I As Long, S() As String Set regex = CreateObject("vbscript.regexp") With regex .Global = True .Pattern = """([^""\r\n]*)""|([^,\r\n]+)" If .test(line) = True Then Set matchcol = .Execute(line) ReDim S(0 To matchcol.Count - 1) I = 0 'matches surrounded by quotes will be in 0 'matches without quotes will be in 1 For Each match In matchcol With match S(I) = .submatches(0) & .submatches(1) End With I = I + 1 Next match End If End With splitLine = S End Function