2016-12-11 95 views
0

通過以下代碼,我從一個morningstar.com CSV文件導入數據,數據由逗號分隔。一些數據包含逗號的問題。vba分割數據,但逗號跳過引號

例如,「XX,XXX」。 這種情況的結果是:

 
cell1(X1,Y1)="XX  
cell(X1,Y2)=XXX" 
instead of: 
cell1(X1,Y1)=XX,XXX 

我VBA

Sub GetKeyRatios() 
Dim URL As String, csv As String, Lines, Values 
Dim i As Long, j As Long, WinHttpReq As Object 
Dim rngStart As Range 

URL = "http://financials.morningstar.com/ajax/exportKR2CSV.html?&callback=?&t=XNYS:JNJ&region=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") 

For i = 0 To UBound(Lines) 
    Values = Split(Lines(i), ",") 
    For j = 0 To UBound(Values) 
     rngPaste.Offset(i, j).Value = Values(j) 
    Next j 
Next i 
End Sub 

有沒有辦法做到這一點? attached example

+1

可以打開'.csv'直接'workbooks.Open(「http://financials.morningstar.com/ajax/exportKR2CSV.html?&callback=?&t=XNYS:JNJ®ion=usa&culture=en- US&cur = USD&order = asc「)' – Slai

+0

你可以發佈一些原始數據嗎? – nightcrawler23

+0

好主意,它的效果很好 –

回答

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&region=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 
+1

非常感謝,代碼完美地工作,因爲我想要的.THANK –

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 
+0

謝謝。由Rowen Chumacera提供的下面的代碼完美地工作 –

相關問題