Find VBA

jtodd

Board Regular
Joined
Aug 4, 2014
Messages
194
Hi
I have this bit of code

Sub find()
rngY = ActiveCell.Value

Sheets("StkFile").Select
Columns("A:F").Select
Selection.find(What:=rngY, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

ActiveCell.Select
End Sub

Which works fine if there is a match if not it comes up with a debug message.

How can I get it to work even if there is no match and give the user some kind of message - not found!

Also is there a way I could get it to work on double clicking a cell and finding that value ??
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
See if this works putting into the 'ThisWorkbook' module

Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

Set Rng = Sheets("StkFile").Columns("A:F").Find(What:=Target.Value, After:=Sheets("StkFile").Range("F" & Rows.Count), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not Rng Is Nothing Then
    Application.Goto Rng
Else
    MsgBox "not found"
End If

End Sub
 
Upvote 0
See if this works putting into the 'ThisWorkbook' module

Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

Set Rng = Sheets("StkFile").Columns("A:F").Find(What:=Target.Value, After:=Sheets("StkFile").Range("F" & Rows.Count), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not Rng Is Nothing Then
    Application.Goto Rng
Else
    MsgBox "not found"
End If

End Sub

Many thanks , worked a charm (y)
 
Upvote 0

Forum statistics

Threads
1,216,185
Messages
6,129,388
Members
449,506
Latest member
nomvula

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top