简体   繁体   English

将转换后的json字符串粘贴到另一个工作表时,出现VBA运行时错误1004

[英]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.

相关问题 在flutter项目中粘贴转换成dart的json代码时出错 - Error when pasting json code converted into dart in flutter project JSON服务字符串错误无法转换为JSONObject - Error for json Service String cannot be converted to JSONObject 尝试从字符串创建JSON对象时出现错误“ []无法转换为JSON” - Error “[] cannot be converted to JSON” when trying to create a JSON Object from String 当我创建数组并将其转换为json字符串时,JSON字符串无效 - Json string not valid when I created an array and converted into json string Android Json解析字符串无法转换为json对象错误 - Android Json parsing string cannot be converted to json object error Android json显示String无法转换为json数组错误 - Android json shows String cannot converted to json array error json,ajax和php在将转换后的json打印为字符串时出错 - json, ajax and php Error printing a converted json to string 解析数据org.json.JSONException的另一个错误:值 - Another Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject json_encode错误类stdClass的对象无法转换为字符串 - json_encode error Object of class stdClass could not be converted to string AsyncTask#3解析JSON对象时出错。 字符串不能转换为JSONObject - AsyncTask #3 error parsing JSON object. String cannot be converted to JSONObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM