[英]Excel Input Box VBA Error Checking Problem
Below is the code that I changed. 下面是我更改的代码。 I cannot figure out VBA for the life of me.
我无法终生了解VBA。 If this was c++ it would have taken me 30 seconds to write.
如果这是c ++,那将花费我30秒钟来编写。 I am still getting the errors.
我仍然遇到错误。
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
If Cells.Find
fails it returns Nothing
. 如果
Cells.Find
失败,则返回Nothing
。 So you need to assign it to a variable, and check its value before trying to .Activate
it. 因此,您需要将其分配给变量,并在尝试对其进行
.Activate
之前检查其值。
In fact you should also check the return value of InputBox
in case Cancel was clicked. 实际上,如果单击“取消”,您还应该检查
InputBox
的返回值。
EDIT: Still contains a number of errors. 编辑:仍然包含许多错误。
Cells.Find
returns a Range
, but you are trying to assign it to a String
variable. Cells.Find
返回一个Range
,但是您正在尝试将其分配给String
变量。 (Also don't forget that Range
and String
variables have different assignment statements.) Range
和String
变量具有不同的赋值语句。) False
instead of checking that it isn't Nothing
. False
进行比较,而不是检查其是否为Nothing
。 Range
rather than trying to find it again. Range
而不是尝试再次找到它。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.