I am attempting to run a program which should pull in data from a spreadsheet, split the data in to chunks, and then import it in to my table based on it's "Values" variant. The format of the data pulled in would look something like this:
"HL","Hecla Mining Company Mining Stock","NSM",12.52,8.69,14.07,6.18
The code for splitting lines, defining values, and assigning them to columns is currently written as follows:
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
The issue occurs in that some of the lines return a name that includes a comma within it such as this:
"CDE","Coeur Mining, Inc.","NSM",7.59,16.25,9.52,7.01
This is causing Values(2) = "Coeur Mining" and Value(3) = "Inc." as opposed to Values(2) = "Coeur Mining, Inc." and Values(3) = "NSM"
I have tried updating the code to the following:
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
However it does not appear to be functioning even with the nested If statement looking for ",Inc." within the sLine string.
Is there a formatting issue that I am not getting? I attempted to use a regex function as well but I am very new to excel/VBA and was unable to get it to figure out how to format it properly.
The regex code that was suggested was given as follows:
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)
Any help would be greatly appreciated, more information or a copy of the actual excel file could be provided upon request.