Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default using the Find method in a VBA macro

I have got a working example of using the Find method in a VBA macro to get
the cell row of the value being searched for, but it only works if the value
being searched for exists in the specified range. If the value being
searched for is not in the range, then a runtime error occurs.
My test contains a worksheet with the numbers 1 to 12 in cells A8 to A19.

Here is my macro;
Sub findRow()
' Worksheet contains the numbers 1 to 12 in cells A8 to A19.

Dim lngMaxRow As Long
Dim Row

Range("A9").Select
Selection.End(xlDown).Select
lngMaxRow = ActiveCell.Row

Range("A8:A" & lngMaxRow).Select

'Look for 4
Row = Selection.Find(what:="4", LookIn:=xlValues, LookAt:=xlWhole).Activate
If Row < "" Then
MsgBox "Found at row " & ActiveCell.Row
End If

'Look for 15
Row = Selection.Find(what:="15", LookIn:=xlValues, LookAt:=xlWhole).Activate
If Row < "" Then
MsgBox "Found at row " & ActiveCell.Row
End If

End Sub

The Find method works fine for the search of "4" whcih it finds at row 11,
but instead of simply not finding the value "15", it causes a runtime error
"Object variable or With block variable not set".

How can I use the Find method so that this error doesn't occur just because
it can't find that particular value?
I was hoping it would return zero or false or something similar to indicate
that the value wasn't found.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default using the Find method in a VBA macro

Hi Mudd,

Try something like:

Sub findRow()
Dim rng As Range, rng1 As Range
Dim searchStr As Variant
Dim msg As String

searchStr = "4"

With ActiveWorkbook.Worksheets("Sheet1")
Set rng = .Range(Range("A9"), Range("A9").End(xlDown))
End With

Set rng1 = rng.Find(searchStr, LookIn:=xlValues, _
LookAt:=xlWhole)
If Not rng1 Is Nothing Then
msg = "The search value """ & searchStr _
& """ found at " & rng1.Address
Else
msg = "The search value """ & searchStr & """ not found"
End If
MsgBox msg, vbInformation, "Search Result"
End Sub


---
Regards,
Norman



"Mudd" wrote in message
...
I have got a working example of using the Find method in a VBA macro to get
the cell row of the value being searched for, but it only works if the
value
being searched for exists in the specified range. If the value being
searched for is not in the range, then a runtime error occurs.
My test contains a worksheet with the numbers 1 to 12 in cells A8 to A19.

Here is my macro;
Sub findRow()
' Worksheet contains the numbers 1 to 12 in cells A8 to A19.

Dim lngMaxRow As Long
Dim Row

Range("A9").Select
Selection.End(xlDown).Select
lngMaxRow = ActiveCell.Row

Range("A8:A" & lngMaxRow).Select

'Look for 4
Row = Selection.Find(what:="4", LookIn:=xlValues,
LookAt:=xlWhole).Activate
If Row < "" Then
MsgBox "Found at row " & ActiveCell.Row
End If

'Look for 15
Row = Selection.Find(what:="15", LookIn:=xlValues,
LookAt:=xlWhole).Activate
If Row < "" Then
MsgBox "Found at row " & ActiveCell.Row
End If

End Sub

The Find method works fine for the search of "4" whcih it finds at row 11,
but instead of simply not finding the value "15", it causes a runtime
error
"Object variable or With block variable not set".

How can I use the Find method so that this error doesn't occur just
because
it can't find that particular value?
I was hoping it would return zero or false or something similar to
indicate
that the value wasn't found.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find method Alan Beban[_2_] Excel Programming 0 September 22nd 04 09:38 PM
Find method Ron de Bruin Excel Programming 0 September 22nd 04 09:25 PM
Find Method LiSa Excel Programming 4 August 17th 04 04:10 PM
The find method Stuart[_6_] Excel Programming 0 August 5th 03 03:14 PM
The Find Method Dick Kusleika Excel Programming 3 July 16th 03 07:59 PM


All times are GMT +1. The time now is 04:00 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"