[英]VBA Runtime Error 1004 when pasting converted json string to another sheet
I have a converted json string response text from a web service call. 我有一个来自Web服务调用的转换后的json字符串响应文本。 The code works if I paste it on the same sheet where the button that calls the web service is. 如果将代码粘贴到调用Web服务的按钮所在的同一张纸上,则该代码有效。 But if I try it to put the result on another sheet I am getting "Error 1004 Application Defined or Object Defined error" 但是,如果我尝试将结果放在另一张纸上,则会收到“错误1004应用程序定义或对象定义的错误”
The code for the click button on excel is as follows; excel中单击按钮的代码如下:
Private Sub webcall_Click()
Dim MyRequest As Object
Dim JSON As Dictionary
Dim Header As Range
Dim Env As String
Logix = Sheets("Sheet1").Cells(2, "G").Value
If Env = "" Then
URL = "https://defaultURL/sqlquery.aspx"
MsgBox (URL)
Else
URL = "https://anotherURL/sqlquery.aspx"
MsgBox (URL)
End If
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "POST", URL
MyRequest.Send
Set JSON = JsonConverter.ParseJson(MyRequest.responseText)
Dim Values As Variant
ReDim Values(JSON("chargebackdept table").Count, 3)
Dim Value As Dictionary
Dim i As Long
i = 0
For Each Value In JSON("chargebackdept table")
Values(i, 0) = Value("chargebackcategory")
Values(i, 1) = Value("chargebackdeptid")
Values(i, 2) = Value("name")
i = i + 1
Next Value
Sheets("Sheet1").Range(Cells(1, "B"), Cells(JSON("chargebackdept table").Count, "D")) = Values
Sheets("Sheet1").Range("B1").Insert Shift:=xlDown
Sheets("Sheet1").Range("C1").Insert Shift:=xlDown
Sheets("Sheet1").Range("D1").Insert Shift:=xlDown
Set Header = Sheets("Sheet1").Range("B1")
Header.Value = "ChargeBack_Category"
Set Header = Sheets("Sheet1").Range("C1")
Header.Value = "ChargeBack_ID"
Set Header = Sheets("Sheet1").Range("D1")
Header.Value = "ChargeBack_Name"
MsgBox ("Done loading chargeback table")
End Sub
My problem is the line; 我的问题是线路;
Sheets("Sheet1").Range(Cells(1, "B"), Cells(JSON("chargebackdept table").Count, "D")) = Values
If I change it to a different sheet like below it will error out; 如果我将其更改为如下所示的其他工作表,则会出现错误;
Sheets("Sheet2").Range(Cells(1, "B"), Cells(JSON("chargebackdept table").Count, "D")) = Values
Qualify the parent worksheet of the Cells that define the Range. 限定定义范围的单元格的父工作表。
with workSheets("Sheet1")
.Range(.Cells(1, "B"), .Cells(JSON("chargebackdept table").Count, "D")) = Values
end with
Note .Cells
and not Cells
. 注意.Cells
而不是Cells
。 The .
的.
provides the qualifying parent worksheet defined in the With ... End With statement. 提供在With ... End With语句中定义的合格父级工作表。 It could also be, 也可能是
workSheets("Sheet1").Range(workSheets("Sheet1").Cells(1, "B"), workSheets("Sheet1").Cells(JSON("chargebackdept table").Count, "D")) = Values
Typically, the 'code for the click button' is on a private worksheet code sheet and not a public module code sheet. 通常,“单击按钮的代码”在私有工作表代码表上,而不在公共模块代码表上。 In a private worksheet code sheet all unqualified range and cell references default to the worksheet that the code sheet belongs to. 在专用工作表代码表中,所有不合格的范围和单元格引用均默认为该代码表所属的工作表。 You cannot define a range on another worksheet (eg sheet2) with the cells from the default worksheet. 您不能使用默认工作表中的单元格在另一个工作表(例如sheet2)上定义范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.