Excel输入框VBA错误检查问题

时间:2023-02-12 22:29:32

Below is the code that I changed. I cannot figure out VBA for the life of me. If this was c++ it would have taken me 30 seconds to write. I am still getting the errors.

下面是我更改的代码。我无法弄清楚VBA对我的生活。如果这是c ++,那么写我需要30秒。我仍然得到错误。

Sub CodeFinder()

    Dim userInput As String
    Dim errorCheck As String

    userInput = InputBox("Please enter the code to search", "Code Search Engine")

    errorCheck = Cells.Find(What:=userInput, _
                       After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
                       SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                       MatchCase:=False)

    If errorCheck = False Then
        MsgBox ("Error")
    Else
        Cells.Find(What:=userInput, _
                   After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
                   SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                   False).Activate
    End If 

End Sub

2 个解决方案

#1


3  

If Cells.Find fails it returns Nothing. So you need to assign it to a variable, and check its value before trying to .Activate it.

如果Cells.Find失败,则返回Nothing。因此,您需要将其分配给变量,并在尝试之前检查其值。激活它。

In fact you should also check the return value of InputBox in case Cancel was clicked.

实际上,如果单击“取消”,您还应该检查InputBox的返回值。

EDIT: Still contains a number of errors.

编辑:仍然包含许多错误。

  1. Cells.Find returns a Range, but you are trying to assign it to a String variable. (Also don't forget that Range and String variables have different assignment statements.)
  2. Cells.Find返回一个Range,但您尝试将其分配给String变量。 (另请不要忘记Range和String变量具有不同的赋值语句。)

  3. You then try to compare the variable to False instead of checking that it isn't Nothing.
  4. 然后尝试将变量与False进行比较,而不是检查它是否为Nothing。

  5. You then need to activate the found Range rather than trying to find it again.
  6. 然后,您需要激活找到的Range而不是尝试再次找到它。

#2


1  

Sub CodeFinder()

    Dim userInput As String
    Dim rFound As Range

    userInput = InputBox("Please enter the code to search", "Code Search Engine")

    If Len(userInput) > 0 Then
        Set rFound = ActiveSheet.Cells.Find(What:=userInput, _
                           After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
                           SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                           MatchCase:=False)

        If Not rFound Is Nothing Then
            rFound.Select
        Else
            MsgBox "No cells found"
        End If
    End If

End Sub

#1


3  

If Cells.Find fails it returns Nothing. So you need to assign it to a variable, and check its value before trying to .Activate it.

如果Cells.Find失败,则返回Nothing。因此,您需要将其分配给变量,并在尝试之前检查其值。激活它。

In fact you should also check the return value of InputBox in case Cancel was clicked.

实际上,如果单击“取消”,您还应该检查InputBox的返回值。

EDIT: Still contains a number of errors.

编辑:仍然包含许多错误。

  1. Cells.Find returns a Range, but you are trying to assign it to a String variable. (Also don't forget that Range and String variables have different assignment statements.)
  2. Cells.Find返回一个Range,但您尝试将其分配给String变量。 (另请不要忘记Range和String变量具有不同的赋值语句。)

  3. You then try to compare the variable to False instead of checking that it isn't Nothing.
  4. 然后尝试将变量与False进行比较,而不是检查它是否为Nothing。

  5. You then need to activate the found Range rather than trying to find it again.
  6. 然后,您需要激活找到的Range而不是尝试再次找到它。

#2


1  

Sub CodeFinder()

    Dim userInput As String
    Dim rFound As Range

    userInput = InputBox("Please enter the code to search", "Code Search Engine")

    If Len(userInput) > 0 Then
        Set rFound = ActiveSheet.Cells.Find(What:=userInput, _
                           After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
                           SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                           MatchCase:=False)

        If Not rFound Is Nothing Then
            rFound.Select
        Else
            MsgBox "No cells found"
        End If
    End If

End Sub