空列表框时出错 [英] Error when empty listbox

查看:45
本文介绍了空列表框时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的以下代码有两个问题;它可以正常工作,直到列表框上没有任何内容显示为止,然后我收到一条错误消息;第二个是我无法刷新列表框上的数据.

Im having two issues with the code below; it works just fine until there is nothing to show on the listbox, the I get an error message; and the second is I cant get It the refresh the data on the 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

推荐答案

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

place a count check on the range you want to fill listbox with, like follows:

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

这篇关于空列表框时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆