根据条件创建阵列

时间:2019-05-17 10:23:06

标签: excel vba

我有以下代码,正在执行一些if,但是使用值却在努力查看如何创建并将这些值添加到数组中。目前,我只是将值添加到列表框中

List(lC, 0) = sh1.Cells(row, 23)

我尝试创建一个整数,然后使用

var = var & List(lC, 0) = sh1.Cells(row, 23)

但是我不确定这是否正确吗?

  Private Sub CommandButton3_Click()
    Dim sh1
    Dim LR
    Dim lC
    Dim row


    Me.lstUsedRooms.Clear

    Set sh1 = ThisWorkbook.Worksheets(4) 'room order from sheets
        With sh1
            LR = .Range("A" & .Rows.Count).End(xlUp).row
        End With

        lC = 0

        With Me.lstUsedRooms
            .ColumnCount = 1 'there is 8 columns
            .RowSource = ""
            .ColumnWidths = 40

        For row = 2 To LR


            NewIVTime = Format("14:00", "h:mm:ss")

                If Left(sh1.Cells(row, 6), 10) = "24/05/2019" Then   ' Gets all interviews for the date specified
                         Dim LTime As Date
                         Dim LTime1 As Date
                         LTime = Format(sh1.Cells(row, 7), "h:mm:ss") 'Gets the times from all the rooms from the date stated above
                         LTime1 = CDate(LTime) + 3 / 24 ' Adds 3 hours to the time above

                         If LTime1 < NewIVTime Then  ' Check which interviews display three hours after the new interview

                            .AddItem
                            .List(lC, 0) = sh1.Cells(row, 23)

                            lC = lC + 1

                        End If

                End If

            Next

            If .ListCount = 0 Then
                Me.lstUsedRooms.ColumnWidths = 100
                Me.lstUsedRooms.AddItem "No Rooms"
            End If


        End With

End Sub

1 个答案:

答案 0 :(得分:0)

首先查看要输入的内容,如果只需要一维数组,则最好的选择是集合:     将newCollection设为新收藏

For each r in Range

newCollection.Add Value 'Add value here 

Next r

如果您需要多维数组,那么数组函数是最好的方法:     昏暗的zArray()作为变体     Redim zArray(x,y,...)'x和y是数组的大小 要么     Redim保留zArray(x,y,...)'如果您遍历Redim

For i = 1 to x
For j = 1 to y

zArray(x,y)

Next j
Next i