【问题标题】:Cant't assign to array vba无法分配给数组 vba
【发布时间】:2016-01-28 14:59:42
【问题描述】:

我的代码有问题,我不知道为什么......在这行“Key = decode.GetKeys(issue)”中引发了这个问题标题中提到的错误。

Public Sub Import_JSON_From_URL(url As JiraJSONGet)

ThisWorkbook.Sheets("RawData").Activate
result = url.LoadJson

Dim total As Long
total = getLastRow()


doInit
Dim Keys() As String
Keys = decode.GetKeys(JsonObject)
Dim issues As Object
Set issues = decode.GetObjectProperty(JsonObject, Keys(4))
Dim field, issue_project, issue_type, issue_status, issue_summary, issue_report, issue_created, issue_updated, issue_assignee, issue_priority, issue_resolution, issue_resolved, issue_time_spent, issue_time_estimated, issue_project_type As Object

Dim Key(), k_fields(), k_project(), k_issuetype(), k_status(), k_summary(), k_report(), k_created(), k_updated(), k_assignee(), k_priority(), k_resolution(), k_resolved(), k_timespent(), k_timeestimated(), k_projecttype() As String

Dim issue, ki, kf, kproject, kissuetype, kstatus, ksummary, kreport, kcreated, kupdated, kassignee, kpriority, kresolution, kresolved, ktimespent, ktimeestimated As Variant

Dim project_name_issue, project_key_issue, key_issue, issue_type_name, issue_type_description, status_name_issue, summary_key, report_key, created, updated, assignee, priority, resolution, resolved, timespent, timeestimated, today As String
For Each issue In issues
        Key = decode.GetKeys(issue)
        For Each ki In Key
            If ki = "key" Then
                key_issue = decode.GetProperty(issue, ki)
                ThisWorkbook.Sheets("RawData").Range("A" & total + 1).value = key_issue
            End If
            If ki = "fields" Then
                Set field = decode.GetObjectProperty(issue, ki)
                k_fields = decode.GetKeys(field)
                For Each kf In k_fields

我有一个名为 JSONDecoder 的类,其中包含 GetKeys 函数:

Public Function GetKeys(ByVal JsonObject As Object) As String()
    Dim length As Integer
    Dim KeysArray() As String
    Dim KeysObject As Object
    Dim index As Integer
    Dim Key As Variant
    Dim value As Variant

    Set KeysObject = ScriptEngine.Run("getKeys", JsonObject)
    length = GetProperty(KeysObject, "length")
    ReDim KeysArray(length - 1)
    index = 0
    For Each Key In KeysObject
        KeysArray(index) = Key
        index = index + 1
    Next
    GetKeys = KeysArray
    'value = GetProperty(JsonObject, "issues")
    'Set value = GetObjectProperty(JsonObject, "issues")

End Function

【问题讨论】:

  • 你从哪里得到错误,你能引用错误描述吗?
  • 发生错误的实际行是什么?此外,在 VBA 中,始终为您声明的所有内容指定数据类型,因为它可能会导致微妙的问题。请参阅this answer 了解有关它如何导致细微问题的详细信息。
  • 这里:Key = decode.GetKeys(issue)
  • 也发布 GetKeys 的代码。我们不是通灵者:)
  • 仅供参考 dim a, b, c as T 之后仅 c 如果类型为 T 其余部分是变体。正确:dim a as T, b as T, c as T

标签: arrays json vba excel


【解决方案1】:

不确定您的问题出在哪里。 我测试了从常规函数和使用类返回一个字符串数组,两者都工作正常,但是你声明你的代码甚至不会编译。 您的问题一定出在其他地方。

模块代码:

Sub Test()

    Dim result() As String

    result = Test1

    Dim i As Integer

    For i = 0 To 3
        Debug.Print result(i)
    Next

    Dim c As New clsTest

    result = c.CreateStrings

    For i = 0 To 3
        Debug.Print result(i)
    Next

End Sub

Function Test1() As String()
    Dim s() As String

    ReDim s(3)

    Dim i As Integer

    For i = 0 To 3
        s(i) = "blah" & i
    Next

    Test1 = s

End Function

类代码:

Public Function CreateStrings() As String()
    Dim r() As String
    Dim i As Integer

    ReDim r(3)

    For i = 0 To 3

        r(i) = "class blah" & i

    Next

    CreateStrings = r

End Function

【讨论】:

    猜你喜欢
    • 2017-01-26
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多