VBA:查找后,FindNext工作几次,然后运行时错误'380'

时间:2018-06-08 11:32:26

标签: vba excel-vba excel

起初我遇到了使用下一行/上一行信息重新加载UserForm的问题,但我找到了一种解决方案,您可以在下面看到。所以这可能是一个问题。

我在第一个UserForm中给出变量,然后查找:

Private Sub CommandButtonFIND_Click()
'on module public variables, e.g.
'Public NeedText As String
'Public KOrow As Variant
'Public wHat As String
'Public fInal As String
'Public wHere As Variant
wHat = "Ordered"
wHere = "AB"
fInal = x1Sheet.UsedRange.Rows.Count
Dim FndRng As Range
With x1Sheet.Range(wHere & "2:" & wHere & fInal)
    Set FndRng = .Find(What:=wHat, LookIn:=xlValues, 
MatchCase:=False)
If Not FndRng Is Nothing Then ' found it!
    KOrow = FndRng.Row
    NeedText = "Text " & x1Sheet.Range("A" & KOrow) & "/" &
x1Sheet.Range("B" & KOrow)
    UserFormMT.Show
    UserFormMT.MultiPage2.SetFocus
    Me.Hide
Else
    MsgBox "No more rows to handle"
End If
End With
End Sub

在UserFormMT上有以下内容:

Private Sub CommandButtonNEXT_Click()
If wHat = "" Then
    KOrow = KOrow + 1
Else
    With x1Sheet.Range(wHere & KOrow + 1 & ":" & wHere & fInal)
    Set FndRng = .Find(What:=wHat, LookIn:=xlValues,
      searchdirection:=xlNext, MatchCase:=False)
    If Not FndRng Is Nothing Then 'found next!
        KOrow = FndRng.Row
        NeedText = "Text " & x1Sheet.Range("A" & KOrow) & "/" & 
              x1Sheet.Range("B" & KOrow)
          'Tried e.g:
          'Me.Repaint
          'userform_initialize
          'userform_activate
        Unload Me
        UserFormMT.Show
    Else
        CommandButtonNEXT.Visible = False
    End If
    End With
End If
End Sub

这有效 - 有一段时间了。在2-5下次单击第二个UserForm之后,代码在UserFormMT.Show上停止并给出运行时错误'380':无法设置value属性。无效的值属性。

也许我只是没有看到明显的东西,所以我很感激任何帮助。

1 个答案:

答案 0 :(得分:3)

尝试在On Error Resume Next语句之前使用find,如下所示:

On Error Resume Next
Set FndRng = .Find(What:=wHat, LookIn:=xlValues,
  searchdirection:=xlNext, MatchCase:=False)
If Err.Number <> 0 Then Debug.Print Err.Description ' remove in production
On Error Goto 0

find语句如果找不到匹配的值,则会抛出异常

If Not FndRng Is Nothing Then

在下一行代码中将无法捕获它,因为错误已经发生。