0
即使我引用了我在代碼中使用的工作表,我也遇到了此VBA錯誤。我不經常使用VBA,所以這可能是一個簡單的問題。<方法'對象的範圍'_Global'失敗> - 指定工作表時仍出現錯誤
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
法 'Range對象的' _Global」失敗
有誰知道如何解決這個問題?
'strKey'包含的值是什麼?我看到'Range(「A」,strKey)= jsonObj(strKey)''行錯誤。此外,你將不得不使它'.Range(「A」和colNumber)= jsonObj(strKey)' – shahkalpesh
@shahkalpesh查看截圖。我會嘗試你的建議。 –
圖片中的代碼與您在此處發佈的代碼不同。我將發佈下面的代碼。如果您遇到任何錯誤,請檢查並告訴我。 – shahkalpesh