文本字段更改中的错误5

时间:2017-09-06 02:37:19

标签: excel vba excel-vba

在更新另一个名为textoControles的组合框后,我更新并填充名为textoCausas的组合框。第一个组合框textoControles随着它的变化而自我提升。

现在,我收到错误' 5'在这种情况下:

如果我将表ActiveSheet.Name没有数据(空)并将数据输入组合框textoCausas,然后在textoCausas上输入一个字符(只输入任何字符),那么错误' 5'停止宏。

但是,如果表ActiveSheet.Name在第一列中有任何数据,并且我将数据输入textoCausas,则没有错误会停止宏。

我需要一些帮助才能解决此错误。谢谢!

Private Sub textoCausas_AfterUpdate()
Dim ws As Worksheet, controles As Range, planes As Range, utlimafila As Double, numeroCausa As Double, C As Range

Set ws = Worksheets(ActiveSheet.Name)

ultimafila = ws.ListObjects(ActiveSheet.Name).Range.Columns(11).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

If ultimafila <> 8 Then
    With Me.textoControles
        .Clear
        If Not IsError(Application.Match(Me.textoCausas.Value, ws.ListObjects(ActiveSheet.Name).ListColumns(1).DataBodyRange, 0)) Then
            Set C = ws.ListObjects(ActiveSheet.Name).ListColumns(1).DataBodyRange.Find(textoCausas.Value, LookIn:=xlValues, lookat:=xlWhole)
            index = C.Row
            numeroCausa = ws.Cells(index, 11)
            For Each controles In ws.ListObjects(ActiveSheet.Name).ListColumns(2).DataBodyRange
                If controles.Columns(10) = numeroCausa Then
                    If controles.Value <> Empty Then
                        .AddItem controles.Value
                        .List(.ListCount - 1, 1) = controles.Offset(0, 1).Value
                    End If
                End If
            Next controles
        End If
    End With
    With Me.textoPlanes
        .Clear
        If Not IsError(Application.Match(Me.textoCausas.Value, ws.ListObjects(ActiveSheet.Name).ListColumns(1).DataBodyRange, 0)) Then
            Set C = ws.ListObjects(ActiveSheet.Name).ListColumns(1).DataBodyRange.Find(textoCausas.Value, LookIn:=xlValues, lookat:=xlWhole)
            index = C.Row
            numeroCausa = ws.Cells(index, 11)
            For Each planes In ws.ListObjects(ActiveSheet.Name).ListColumns(6).DataBodyRange
                If planes.Columns(6) = numeroCausa Then
                    If planes.Value <> Empty Then
                        .AddItem planes.Value
                        .List(.ListCount - 1, 1) = planes.Offset(0, 1).Value
                    End If
                End If
            Next planes
        End If
    End With
End If

Me.textoControles = Null
Me.textoPlanes = Null
End Sub

Private Sub textoControles_Change()

Dim ws As Worksheet, C As Range, C2 As Range
Set ws = Worksheets(ActiveSheet.Name)

Me.textoEfectividad = Null
Me.textoFrecuencia = Null
Me.textoResponsable = Null

If Trim(Me.textoControles.Value & vbNullString) = vbNullString Then
    Me.textoEfectividad = Null
    Me.textoFrecuencia = Null
    Me.textoResponsable = Null
    Exit Sub
End If


If Not IsError(Application.Match(Me.textoControles.Value, ws.ListObjects(ActiveSheet.Name).ListColumns(2).DataBodyRange, 0)) Then
    Set C = ws.ListObjects(ActiveSheet.Name).ListColumns(2).DataBodyRange.Find(textoControles.Value, LookIn:=xlValues, lookat:=xlWhole)
    index = C.Row
    Set C2 = ws.ListObjects(ActiveSheet.Name).ListColumns(11).DataBodyRange.Find(ws.Cells(index, 11), LookIn:=xlFormulas, lookat:=xlWhole, SearchDirection:=xlPrevious) 'xlFormulas para buscar en celdas ocultas
    index2 = C2.Row
    For i = index To index2
        If ws.Cells(i, 2) = Me.textoControles Then
            Me.textoEfectividad = ws.Cells(i, 3)
            Me.textoFrecuencia = ws.Cells(i, 4)
            Me.textoResponsable = ws.Cells(i, 5)
        Exit For
        End If
    Next i
End If

End Sub

1 个答案:

答案 0 :(得分:1)

它是IsError函数。在VBA中,它告诉变量是否具有值vbError。我昨天想说这个,但我无法复制错误。它似乎不是由Match功能产生的。也许,ListObject不存在于空白工作表上,导致发生错误,该错误不会将vbError的值分配给测试产生的变体。因此,要做的是将Application.Match(Me.textoControles.Value, ws.ListObjects(ActiveSheet.Name).ListColumns(2).DataBodyRang‌​e分配给临时变量。在On Error Resume Next之前加上该行,并使用If Err Then关注该行,您的问题就会消失。