VBA - 将一串csv放入电子表格的最快方法

时间:2017-05-01 21:19:24

标签: excel vba excel-vba csv

您好我试图通过VBA将我从HTTP请求获得的一串csv放入excel电子表格中,即使用

request = CreateObject("WinHttp.WinHttpRequest.5.1")

并将响应传递给字符串对象。我想知道是否有更好的方法,只是通过用新行分割字符串然后再用逗号再分割每一行来加倍循环字符串。

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

Dim csv As String
csv = "" 'get the CSV into this variable

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1, ForWriting = 2
Dim txt As Object
Set txt = fso.OpenTextFile("c:\output.csv", ForWriting, True)
txt.Write csv
txt.Close
Set txt = Nothing

Dim xlApp As New Excel.Application 'This may not be necessary if this code is running within an existing Excel application
xlApp.Workbooks.OpenText "c:\output.csv"

您可能需要对OpenText的参数进行一些调整。

参考文献: