Hi,
I'm trying to use a simple for to search the entire workbook for 1 value. Below you will have the code I currenlty have but I keep getting an error stating that "Oject variable or With block variable not set". Can someone point me in the right direction please. Thank you in advance.
The code in yellow is what "debug" highlights as the issue.
Code
Option Explicit
Dim oWs As Worksheet
Dim rSearch As Range
Dim rCl As Range
Dim sFind As String
Dim sFirstAddress As String
Private Sub cmdFinish_Click()
Unload Me
End Sub
Private Sub cmdSearch_Acive_Click()
If Me.tbxFind.Value = "" Then
MsgBox "No search item entered"
Me.tbxFind.SetFocus
Exit Sub
Else: sFind = Me.tbxFind.Value
End If
If IsNumeric(sFind) Then
Set rSearch = oWs.Range("B2:B500")
End If
'find it
With rSearch
[COLOR="Yellow"] Set rCl = .Find(sFind, LookIn:=xlValues)[/COLOR]
If Not rCl Is Nothing Then
Select Case MsgBox(sFind & " here: " & oWs.Name & " : " & _
rCl.Address & ". Do you want to modify the data?", vbYesNo Or _
vbQuestion Or vbDefaultButton1, "Success")
Case vbYes
rCl.Select
End
Case vbNo
'continue search of sheet
sFirstAddress = rCl.Address
Do
'colour cell if you want
rCl.Interior.ColorIndex = 3
Set rCl = .FindNext(rCl)
Loop While Not rCl Is Nothing And rCl.Address <> sFirstAddress
End Select
End If
End With
End Sub
Private Sub cmdSearch_All_Click()
If Me.tbxFind.Value = "" Then
MsgBox "No search item entered"
Me.tbxFind.SetFocus
Exit Sub
Else: sFind = Me.tbxFind.Value
End If
For Each oWs In ActiveWorkbook.Worksheets
'check for entries in sheets
If Application.WorksheetFunction.CountA(oWs.Cells) > 0 Then
'determine range to search
If IsNumeric(sFind) Then
Set rSearch = oWs.Range("B2:B500")
End If
'find it
With rSearch
[COLOR="yellow"] Set rCl = .Find(sFind, LookIn:=xlValues)[/COLOR]
If Not rCl Is Nothing Then
Select Case MsgBox(sFind & " here: " & oWs.Name & " : " & _
rCl.Address & ". Do you want to modify the data?", vbYesNo _
Or vbQuestion Or vbDefaultButton1, "Success")
Case vbYes
rCl.Select
End
Case vbNo
'continue search of sheet
sFirstAddress = rCl.Address
Do
'colour cell if you want
rCl.Interior.ColorIndex = 3
Set rCl = .FindNext(rCl)
Loop While Not rCl Is Nothing And rCl.Address <> sFirstAddress
End Select
End If
End With
End If
Next oWs
End Sub
Display More