【问题标题】:Creating a jagged array in VBA, writing seems to work but reading fails Type Mismatch在VBA中创建一个锯齿状数组,写入似乎工作但读取失败类型不匹配
【发布时间】:2016-05-25 00:52:16
【问题描述】:

我在 SAP 中,试图将 GuiLabel 的数组转储到锯齿状数组中。

首先我解析孩子的 ID 以获取行和列,同时过滤掉非 GuiLabel 对象。

这部分有效。

我创建了一个表示线条的变体数组,在每个线条元素中我放置了一个字符串数组,表示每个“标签列”。

这部分似乎没有错误

当我尝试从第一个变体数组读回时出现问题。

我关注了这个很棒的帖子 -> How do I set up a "jagged array" in VBA?

到最后,似乎我应该能够使用简单的 Debug.Print myRows(0)(0) 读取我的每个元素,但是我得到类型不匹配!

任何建议表示赞赏!

Function LabelToArray(LblCollection As Variant) As Variant()

    Dim myid As String
    Dim mycolumn As String
    Dim myrow As String
    Dim intLastRow As Integer
    Dim intLastCol As Integer
    Dim myRows As Variant
    Dim myColumns() As String

    ' Create first row
    ReDim myRows(0)

    ' For every child object in the collection
    For Each mychld In LblCollection

        ' Only execute for labels
        If mychld.Type = "GuiLabel" Then

            ' Get column and row of current label
            myid = Split(mychld.ID, "[")(4)
            myid = Mid(myid, 1, Len(myid) - 1)
            mycolumn = Split(myid, ",")(0)
            myrow = Split(myid, ",")(1)

            ' New row ? Reset column counter, set row counter and redim the array of rows
            ' This will fail spectacularly if SAP stop giving rows in numerical order
            If myrow > intLastRow Then intLastCol = 0: intLastRow = myrow: ReDim myRows(myrow)

            ' Reset or resize this line's columns array
            If intLastCol = 0 Then
                ' Set filled out 'myColumns' array into correct 'myRows' object
                If myrow <> 0 Then myRows(myrow - 1) = myColumns
                ' testing, print last column in this row
                If myrow <> 0 Then Debug.Print myColumns(UBound(myColumns))
                ' testing, print first column in this row
                If myrow <> 0 Then Debug.Print myColumns(0)
                ' testing, print number of columns
                If myrow <> 0 Then Debug.Print UBound(myColumns)
                ' Reset myColumns array because we're on a new line
                ReDim myColumns(0)
            Else
                ReDim Preserve myColumns(intLastCol)
            End If

            myColumns(intLastCol) = mychld.Text

            intLastCol = intLastCol + 1

            'Debug.Print mycolumn & "," & myrow

        End If

    Next

    ' Copy last row into array
    myRows(myrow) = myColumns

    ' SEEMS TO WORK FINE UP TO THIS POINT !

    Debug.Print UBound(myRows)
    Debug.Print myRows(0)(0)  'THIS LINE FAILS, TYPE MISMATCH (run-time error 13) ! I also tried cstr(myRows(0)(0))

    LabelToArray = myRows

End Function

【问题讨论】:

  • ReDim myRows(myrow) - 你在这里错过Preserve 吗?
  • 您能添加一个带有输入和预期输出的示例吗?
  • @TimWilliams 你是对的,这会清除阵列!接得好 !我明天下午第一件事就试试这个! (夜班啊!)非常感谢!
  • @FlorentB。我的错误很可能是缺少“保留”语句,每次添加一行时都会清除数组。 LblCollection 包含 SAP GUI 用户区域上的一堆对象。其中一些对象是 GuiLabels(只是 SAP 标签)。标签按行和列排列,但每行有不同数量的“列”。实际上,这只是由标签构成的短语。每个标签都有一个文本标题。 ID 属性类似于“wnd[0]/usr/lblsomething[24,3]”。这将是从第 24 列开始的第 3 行的标签。类似于 80x25 控制台输出。
  • @TimWilliams 就是这样,现在可以了,谢谢!

标签: arrays vba


【解决方案1】:

如果第二个维度不存在,则会引发该错误。我的钱会继续作为你的问题。

这是一个交错数组的示例,最后一个是空的。最后一行会抛出 Type Mismatch 错误,因为没有第二维。

Dim mainArray(0 To 3) As Variant
Dim subArray As Variant

subArray = Array("A", "B", "C")
mainArray(0) = subArray
Debug.Print mainArray(0)(0)
'prints A

subArray = Array(1, 2, 3, 4)
mainArray(1) = subArray
Debug.Print mainArray(1)(0)
'prints 1

subArray = Split("HELLO|WORLD", "|")
mainArray(2) = subArray
Debug.Print mainArray(2)(0)
'prints HELLO

mainArray(3) = Empty
Debug.Print mainArray(3)(0)
'Type mismatch

查看您的 Locals Window 并测试您的数组是否正确。

正如 cmets 中所述,没有 PreserveReDim 看起来很可疑 - 我认为您可能正在清除旧数组。

【讨论】:

  • 谢谢,看来你们已经发现我的错误了,我明天会发布更新!
猜你喜欢
  • 1970-01-01
  • 2015-05-06
  • 2013-04-21
  • 2015-04-23
  • 1970-01-01
  • 2019-10-18
  • 1970-01-01
  • 2021-05-04
  • 2012-06-04
相关资源
最近更新 更多