使用VBA

时间:2019-01-04 20:42:49

标签: excel vba excel-vba csv

当前代码:

' send http post request
objHTTP.Send (payload)

' response is a CSV formatted string
Dim response
response = objHTTP.responseText

If objHTTP.Status = "200" Then 'success
    ' add response to some worksheet
    ThisWorkbook.Names("some_cell").RefersToRange.Value = response
Else
    Debug.Print "Something Went Wrong!"
End If

问题:

整个csv字符串都被添加到单个单元格中(这并不奇怪,因为我的named_range是单个单元格),但是我不知道需要更新什么内容才能使其正常工作。

正确的行为是将CSV字符串添加到工作表中,就像excel打开.csv文件一样。

1 个答案:

答案 0 :(得分:1)

类似这样的东西:

Sub Tester()
    LoadCsvToRange "A,B,C" & vbCrLf & "D,E,F", Sheet1.Range("e5")
End Sub

Sub LoadCsvToRange(csv As String, rngDest As Range)
    Dim f, wb
    'write to a temp file
    With CreateObject("scripting.filesystemobject")
        f = .GetSpecialFolder(2) & "\" & .GetTempName & ".csv"
        .CreateTextFile(f).write csv
    End With
    'open file in Excel and copy content
    With Workbooks.Open(f)
        .Sheets(1).UsedRange.Copy rngDest.Cells(1)
        .Close False
    End With
    Kill f 'remove temp file
End Sub