简体   繁体   中英

Error 5 in text field change

I'm updating and populating a combobox named textoControles after updating another combobox named textoCausas . The first combobox textoControles upadtes itself with its change.

Now, I'm getting error '5' in this scenario:

If I have the table ActiveSheet.Name with no data (empty) and enter data into combobox textoCausas , and then enter a character (just entering any character) on textoCausas , then the error '5' stops the macro.

But, if the table ActiveSheet.Name has any data in the first column and I enter data into textoCausas no error stops the macro.

I need some help to solve this error. Thanks!

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

It's the IsError function. In VBA it tells if a variant has the value vbError. I wanted to say this yesterday but I couldn't replicate the error. It doesn't seem to result from the Match function. Perhaps, the ListObject doesn't exist on a blank sheet causing an error to occur which doesn't assign a value of vbError to the variant resulting from the test. So, the thing to do is to assign Application.Match(Me.textoControles.Value, ws.ListObjects(ActiveSheet.Name).ListColumns(2).DataBodyRang‌​e to a temporary variable. Precede the line with On Error Resume Next and follow it with If Err Then , and your problem should go away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM