错误5在文本字段更改
更新另一个名为textoCausas
combobox后,我正在更新并填充名为textoControles
combobox。 第一个comboboxtextoControles
上升自己的变化。
现在,在这种情况下,我收到错误“5”:
如果我有ActiveSheet.Name
表没有数据(空),并input数据到comboboxtextoCausas
,然后input一个字符(只需input任何字符)在textoCausas
,然后错误'5'停止macros。
但是,如果ActiveSheet.Name
表在第一列中有任何数据,并且我将数据input到textoCausas
没有错误会停止该macros。
我需要一些帮助来解决这个错误。 谢谢!
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
这是IsError函数。 在VBA中,它告诉一个变体是否有值vbError。 我昨天想说这个,但是我不能复制这个错误。 这似乎不是从匹配function的结果。 也许, ListObject
不存在于一张空白纸上,导致一个错误发生,它不会将vbError的值赋值给由testing产生的变体。 因此,要做的事情是将Application.Match(Me.textoControles.Value, ws.ListObjects(ActiveSheet.Name).ListColumns(2).DataBodyRange
给一个临时variables。在On Error Resume Next
前面加上On Error Resume Next
跟着它If Err Then
,你的问题应该消失。