ora*_*dit 3 excel vba find excel-vba
我正在寻找列表中的用户ID#.但是有些用户不再存在.我尝试了test
方法,on error go to
方法和if err.number<> 0 then
方法.我还是收到了Run-time error '91': object variable or with block variable not set
.该号码不存在于列表中.下面是我的代码,有几个徒劳无功的尝试
On Error GoTo errorLn
If Err.Number <> 0 Then
GoTo errorLn
End If
Cells.Find(What:=uSSO, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Select
Run Code Online (Sandbox Code Playgroud)
还有哪些其他选择?或者我错放了"错误"的界限?我在"cells.Find ......"之前和之后尝试过它.
end*_*and 11
大概你会想要做一些不同于消息框的事情.
Dim myCell As Range
Set myCell = Cells.Find(What:=uSSO, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If (Not myCell Is Nothing) Then
MsgBox "something!"
Else
MsgBox "nothing"
End If
Run Code Online (Sandbox Code Playgroud)
我相信你需要稍微调整一下。使用 处理错误不是最佳做法On Error Resume Next
,但您可以尝试以下操作:
On Error Resume Next
Cells.Find(What:=uSSO, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Select
If Err.Number <> 0 Then
'''Do your error stuff'''
GoTo errorLn
Else
Err.Clear
End If
Run Code Online (Sandbox Code Playgroud)
这对你的情况有用吗?
来源:http : //www.mrexcel.com/forum/excel-questions/143988-check-if-value-exists-visual-basic-applications-array.html
尝试这个
Sub Sample1()
Dim oSht As Worksheet
Dim uSSO As String
Dim aCell As Range
On Error GoTo Whoa
'~~> Change this to the relevant sheet
Set oSht = Sheets("Sheet1")
'~~> Set User ID here
uSSO = "User ID"
Set aCell = oSht.Cells.Find(What:=uSSO, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> Check if found or not
If Not aCell Is Nothing Then
MsgBox "Value Found in Cell " & aCell.Address
Else
MsgBox "Value Not found"
End If
Exit Sub
Whoa:
MsgBox Err.Description
End Sub
Run Code Online (Sandbox Code Playgroud)
我还建议阅读此链接,我已经涵盖.Find
和.FindNext
主题:Excel VBA 中的 .Find 和 .FindNext
链接:http : //www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/
归档时间: |
|
查看次数: |
70071 次 |
最近记录: |