我正在寻找从列表中的用户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
有什么其他的选择有哪些? 还是我乱放“错误”的线路? 我之前和后已经尝试过了“cells.Find ......”
我相信你会需要重组它只是一点点。 这不是处理有错误的最佳实践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
是否适合自己情况的工作?
来源 : http://www.mrexcel.com/forum/excel-questions/143988-check-if-value-exists-visual-basic-applications-array.html
你会想要做的东西比有消息框,想必不同。
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
试试这个
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
我还建议阅读此链接,我已经涵盖.Find
和.FindNext
主题:.Find和.FindNext在Excel VBA
链接 : http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/
只是给后人,这是你如何做到这一点瓦特/错误处理(来源: http://www.mrexcel.com/forum/excel-questions/519070-visual-basic-applications-error-handling-when-dealing-细胞-find.html )。
Dim rngFound As Range
Set rngFound = Sheets("WhateverSheet").UsedRange.Find(What:="SoughtValue",LookIn:=xlFormulas)
If Not rngFound Is Nothing Then
'you found the value - do whatever
Else
' you didn't find the value
End if