ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Find Problem (https://www.excelbanter.com/excel-programming/373829-find-problem.html)

John

Find Problem
 
Hi,

Using Code to find a value, and if found, then delete the line.
Simple right?


Cells.Find(what:="2000103", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

If ActiveCell.Value = 2000103 Then
Selection.EntireRow.Delete

End If


But problem is, when the value 2000103 is not present, I get Run Time Error
91.

Any ideas on debugging?


Ron de Bruin

Find Problem
 
Hi

See the example below that use Set rng and check it with
If Not rng Is Nothing

Sub Find_First()
Dim FindString As String
Dim rng As Range
FindString = InputBox("Enter a Search value")
If Trim(FindString) < "" Then
With Sheets("Sheet1").Range("A:A")
Set rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
Application.Goto rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub

You can also use this
http://www.rondebruin.nl/delete.htm#Find


--
Regards Ron de Bruin
http://www.rondebruin.nl



"John" wrote in message ...
Hi,

Using Code to find a value, and if found, then delete the line.
Simple right?


Cells.Find(what:="2000103", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

If ActiveCell.Value = 2000103 Then
Selection.EntireRow.Delete

End If


But problem is, when the value 2000103 is not present, I get Run Time Error
91.

Any ideas on debugging?




John

Find Problem
 
Thanks Ron Big Help
Not exactly sure what its doing but hey it works.
Thanks
John

"Ron de Bruin" wrote:

Hi

See the example below that use Set rng and check it with
If Not rng Is Nothing

Sub Find_First()
Dim FindString As String
Dim rng As Range
FindString = InputBox("Enter a Search value")
If Trim(FindString) < "" Then
With Sheets("Sheet1").Range("A:A")
Set rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
Application.Goto rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub

You can also use this
http://www.rondebruin.nl/delete.htm#Find


--
Regards Ron de Bruin
http://www.rondebruin.nl



"John" wrote in message ...
Hi,

Using Code to find a value, and if found, then delete the line.
Simple right?


Cells.Find(what:="2000103", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

If ActiveCell.Value = 2000103 Then
Selection.EntireRow.Delete

End If


But problem is, when the value 2000103 is not present, I get Run Time Error
91.

Any ideas on debugging?






All times are GMT +1. The time now is 01:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com