空列表框时出错

时间:2016-08-28 22:41:56

标签: vba listbox

我在下面的代码中遇到两个问题;它工作正常,直到列表框上没有任何内容显示,我收到一条错误消息;第二个是我无法让它刷新列表框中的数据。

这是我得到的。

Private Sub UserForm_Initialize()

Dim tiempoa As Date
Dim tiempob As Date
Dim tiempoc As Date

Dim seguimiento As Long, i As Long
Dim Data() As Variant 
Dim cell As Range


With Me.ListBox1
    .ColumnCount = 6
    .ColumnWidths = "40;82;117;117;60;180"
End With

i = 1
With Hoja2 
    With .Range("T2:T" & .Cells(.Rows.Count,"B").End(xlUp).Row).SpecialCells(xlCellTypeConstants) 
        seguimiento = .Count 
        ReDim Data(1 To seguimiento + 1, 1 To Me.ListBox1.ColumnCount) 

        Data(1, 1) = "FOLIO"
        Data(1, 2) = "NOMBRE"
        Data(1, 3) = "APELLIDO PATERNO"
        Data(1, 4) = "APELLIDO MATERNO"
        Data(1, 5) = "HORAS"
        Data(1, 6) = "DIAGNOSTICO DE TRIAGE"

        For Each cell In .Cells
            i = i + 1
            With cell
                Data(i, 1) = .Offset(, -19) 
                Data(i, 2) = .Offset(, -17) 
                Data(i, 3) = .Offset(, -16) 
                Data(i, 4) = .Offset(, -15) 
                            tiempoa = .Offset(, -18).Value
                            tiempob = Now
                            tiempoc = Format(tiempoa - tiempob, "hh:mm")
                Data(i, 5) = tiempoc 
                Data(i, 6) = .Offset(, -3) 
            End With
        Next cell
    End With
End With

ListBox1.List = Data 
End Sub

1 个答案:

答案 0 :(得分:1)

对要填充列表框的范围进行计数检查,如下所示:

Option Explicit

Private Sub UserForm_Initialize()

    Dim tiempoa As Date
    Dim tiempob As Date
    Dim tiempoc As Date

    Dim seguimiento As Long, i As Long
    Dim Data() As Variant
    Dim cell As Range


    With Me.ListBox1
        .ColumnCount = 6
        .ColumnWidths = "40;82;117;117;60;180"
    End With

    i = 1
    With Hoja2
        With .Range("T2:T" & .Cells(.Rows.Count, "B").End(xlUp).row)

            If WorksheetFunction.Count(.Cells) = 0 Then Exit Sub '<--| if no cells in referenced range then exit sub

            With .SpecialCells(xlCellTypeConstants)
                seguimiento = .Count
                ReDim Data(1 To seguimiento + 1, 1 To Me.ListBox1.ColumnCount)

                Data(1, 1) = "FOLIO"
                Data(1, 2) = "NOMBRE"
                Data(1, 3) = "APELLIDO PATERNO"
                Data(1, 4) = "APELLIDO MATERNO"
                Data(1, 5) = "HORAS"
                Data(1, 6) = "DIAGNOSTICO DE TRIAGE"

                For Each cell In .Cells
                    i = i + 1
                    With cell
                        Data(i, 1) = .Offset(, -19)
                        Data(i, 2) = .Offset(, -17)
                        Data(i, 3) = .Offset(, -16)
                        Data(i, 4) = .Offset(, -15)
                                    tiempoa = .Offset(, -18).value
                                    tiempob = Now
                                    tiempoc = Format(tiempoa - tiempob, "hh:mm")
                        Data(i, 5) = tiempoc
                        Data(i, 6) = .Offset(, -3)
                    End With
                Next cell
            End With
        End With
    End With

    Me.ListBox1.List = Data

End Sub