通过以下代码我从morningstar.com CSV文件导入数据,数据用逗号分隔。某些数据包含逗号的问题。
例如,“XX,XXX”。 这种情况的结果是:
cell1(X1,Y1)="XX cell(X1,Y2)=XXX" instead of: cell1(X1,Y1)=XX,XXX
我的VBA
for (int k = 0; k < namelength; k++)
{
sorted(ref testgrades);
Console.WriteLine(name[nameindex] + testgrades[k]);
}
有没有办法做到这一点?
例如
答案 0 :(得分:0)
您可以尝试使用","
替换所有出现的;
(包括引号)。我在第一次操作后也替换了代码中的所有引号。然后你可以分开;获得每个号码。
Sub m()
Dim str As String
'string to split
' Value in A10 is "53,234","45,568","99,999"
'just an example
str = Range("A10").Value ' in your case will be = Lines(i)
'replace all occurences of "," with ;
str = Replace(str, """" & "," & """", ";")
'replace any extra "
str = Replace(str, """", "")
MsgBox (str)
End Sub
答案 1 :(得分:0)
你可以试试这个。我添加了一些变量并更正了您的声明rngPaste
(您声明为rngStart
)。
Sub GetKeyRatios()
Dim URL As String, csv As String, Lines() As String
Dim Values() As String
Dim i As Long, j As Long, WinHttpReq As Object
Dim k As Integer, l As Integer 'Added to separate row and column numbering
Dim rngPaste As Range
Dim revenue 'Added for concatenation of revenue values
URL = "http://financials.morningstar.com/ajax/exportKR2CSV.html?&callback=?&t=XNYS:JNJ®ion=usa&culture=en-US&cur=USD&order=asc"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", URL, False
WinHttpReq.send
csv = WinHttpReq.responseText
Lines = Split(csv, vbLf)
Set rngPaste = Sheets("KeyRatios").Range("A1")
k = 0
For i = 0 To UBound(Lines)
Values = Split(Lines(i), ",")
l = 0
If UBound(Values) > 0 Then
For j = 0 To UBound(Values)
If Values(j) = "" Then
l = l + 1
ElseIf Mid(Values(j), 1, 1) = Chr(34) Then
revenue = Mid(Values(j), 2)
ElseIf Mid(Values(j), Len(Values(j)), 1) = Chr(34) Then
revenue = revenue & "," & Mid(Values(j), 1, Len(Values(j)) - 1)
rngPaste.Offset(k, l).Value = revenue
l = l + 1
Else
rngPaste.Offset(k, l).Value = Values(j)
l = l + 1
End If
Next j
ElseIf UBound(Values) = 0 Then
rngPaste.Offset(k, l).Value = Values(0)
End If
k = k + 1
Next i
End Sub