0

即使我正在引用我在代码中使用的工作表,我也会收到此 VBA 错误。我不经常使用 VBA,所以这可能是一个简单的问题。

Sub ParseJSON()
    Dim jsonText As String
    Dim jsonObj As Dictionary
    Dim jsonRows As Collection
    Dim jsonRow As Collection
    Dim ws As Worksheet
    Dim currentRow As Long
    Dim startColumn As Long
    Dim i As Long

    Set ws = Worksheets("Sheet1")

    'Create a real JSON object
    jsonText = ws.Range("B2").Value

    'Parse it
    Set jsonObj = JSON.parse(jsonText)

    'Set the starting row where to put the values
    currentRow = 5

    'First column where to put the values
    startColumn = 1 'A

    'For i = 1 To jsonObj.Count
     'ws.Cells(currentRow, startColumn) = jsonObj
    'Next i


    For Each strKey In jsonObj.keys()
    With ws
     Range("A", strKey) = jsonObj(strKey)
    End With
    Next

        'ws.Range("A", strKey).Activate = jsonObj(strKey)

    'Get the rows collection
    'agencyID = jsonObj("agencyID")
    'agencyName = jsonObj("agencyName")


    'Set the starting row where to put the values
    currentRow = 1

    'First column where to put the values
    startColumn = 2 'B

    'Loop through all the values received
    'For Each jsonRow In jsonRows
        'Now loop through all the items in this row
        'For i = 1 To jsonRow.Count
            ws.Cells(5, 5) = agencyID
        'Next i

        'Increment the row to the next one
        'currentRow = currentRow + 1
    'Next jsonRow
End Sub

我在这条线上得到了错误

With ws
     Range("A", strKey) = jsonObj(strKey)
    End With

方法'对象范围'_Global'失败

截屏

有谁知道如何解决这个问题?

4

1 回答 1

0
For Each strKey In jsonObj.keys()
   ws.Cells(currentRow, startColumn).Value = jsonObj(strKey)
   currentRow = currentRow + 1
Next

编辑:在您的情况下发生错误,因为密钥可能是agencyID- 这将导致以下代码

Range("A", "agencyID") = "valueOfAgencyID"

Excel 将无法找到带有 address/name 的单元格AagencyID,因此出现错误。

于 2013-08-12T11:15:11.193 回答