VBA - 将多维数组加载到列表框中一次或每秒两次

时间:2017-02-20 15:02:25

标签: vba multidimensional-array listbox

我在Factory Talk中使用VBA,这是一个主要用于HMI自动化系统的软件。工厂谈话使用标签,这些标签是工厂谈话“谈话”的PLC内部数据的地址。这些标签更新率可以从.250秒及以上更改。

一个屏幕有一个列表框,现在重新填充新数据,更新速率为.750秒。我从PLC中检索标签,进行一些格式化,过滤,可能排序,然后使用以下命令将数据从多维数组加载到列表框中:

Listbox1.List = buffer

我以这种方式使用列表框来允许对某些列进行排序。

问题:是否有更快的方法将多维数组加载到列表框中?列表框甚至是用于VBA的正确控件吗?请记住,我使用的是VBA,而不是vb.Net。似乎当我在任何HMI上保持这个屏幕打开几小时或几天(这在生产过程中非常可能)时,服务器内存对于该用户来说真的很高。

以下代码是与此问题相关的例程,其中数据已加载到列表框中。

Private Sub LoadDataIntoBuffer()
Application.LogDiagnosticsMessage ("Load DATA INTO BUFFER start - " & DateTime.Now)
Dim i As Integer               'Loop Counter for Tag Groups
Dim c As Integer: c = 0        'Loop Counter for Buffer Array
Dim p As Integer: p = 1        'Counter for Set of Vehicle Data
Dim pathID As String           'To Hold PathID of Vehicle
Dim position As String         'To Hold Position of Vehicle
Dim TagsInError As StringList
Dim ModuleName As String

'Error management
ModuleName = "LoadDataIntoBuffer"
On Error GoTo Error_Handler

'Erase Stored Values in Buffer
Erase buffer

'Iterate Through Each Vehicle and Vehicle Data into Buffer Array
For i = 1 To maxVehicles
    pathID = MMTagGrpVeh.Item(p + 1).Value
    position = Format(Val(MMTagGrpVeh.Item(p + 2).Value), "0.000000")

    'If PathID and Position are not zero, the vehicle is valid. Store in Buffer Array
    If pathID <> 0 And position <> 0 Then
        buffer(c, 0) = "      " & i
        buffer(c, 1) = MMTagGrpVeh.Item(p).Value
        buffer(c, 2) = pathID
        buffer(c, 3) = position
        buffer(c, 4) = MMTagGrpVeh.Item(p + 3).Value
        buffer(c, 5) = TranslateInt(MMTagGrpVeh.Item(p + 4).Value)

        c = c + 1  'Increment Entry Number
    End If
    p = p + 5
Next

entriesInBuffer = c - 1  'Keep Track of Number of Entries in Buffer Array

'If List is Sorted on a Column, Keep it Sorted on that Column After Updating Values
'Barcode
If listIsSorted = 1 Then
    selectionSortString buffer, 1, entriesInBuffer
'Path Id + Position
ElseIf listIsSorted = 2 Then
    selectionSortPathID buffer, 2, entriesInBuffer
'Vehicle Status
ElseIf listIsSorted = 3 Then
    selectionSortString buffer, 5, entriesInBuffer
'Reverse Vehicle ID
ElseIf listIsSorted = 4 Then
    selectionSortReverseInteger buffer, 0, entriesInBuffer
'Reverse Barcode
ElseIf listIsSorted = 5 Then
    selectionSortReverseString buffer, 1, entriesInBuffer
'Reverse PathID + Position
ElseIf listIsSorted = 6 Then
    selectionSortPathIDReverse buffer, 2, entriesInBuffer
'Reverse Vehicles Status
ElseIf listIsSorted = 7 Then
    selectionSortReverseString buffer, 5, entriesInBuffer
End If

Exit Sub




Private Sub LoadDataIntoListBox()
Application.LogDiagnosticsMessage ("LOAD DATA INTO LISTBOX start - " & DateTime.Now)
Dim listBoxArray(numRowsInListBox - 1, numColumns - 1)   'Array to Hold List Box Data
Dim ModuleName As String   'Error Handling
Dim i As Integer           'Loop Counter for Buffer Array
Dim c As Integer: c = 0    'Loop Counter for ListBox Array
Dim index As Integer

'Error management
ModuleName = "LoadDataIntoListBox"
On Error GoTo Error_Handler

'Iterate through the Indexes of the Buffer Array that will be...
'...Shown in List Box and Put the Data in the List Box Array
For i = lowerBound To upperBound
    If i <= maxVehicles Then
        listBoxArray(c, 0) = buffer(i, 0)
        listBoxArray(c, 1) = buffer(i, 1)
        listBoxArray(c, 2) = buffer(i, 2)
        listBoxArray(c, 3) = buffer(i, 3)
        listBoxArray(c, 4) = buffer(i, 4)
        listBoxArray(c, 5) = buffer(i, 5)

        c = c + 1
    End If
Next i

'Get Current Selected Index of List Box
index = ListBox1.ListIndex

'Send Data in listBoxArray into List Box for Display on Screen
ListBox1.List = listBoxArray

'Set Selected Index of List Box to Previously Stored Index
ListBox1.ListIndex = index
Exit Sub

0 个答案:

没有答案