【发布时间】: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 就是这样,现在可以了,谢谢!