Double Click Find Macro

juke_juke13

Board Regular
Joined
May 25, 2004
Messages
57
I have the following code and can't quite get it to work as it always tells me the value is not found.

Purpose: Double-click on a cell in Sheet1 and have it look for the value in that cell in Sheet2


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)


Dim Search As String

On Error GoTo ErrorCatch
Search = Target.Value

'Sheet2
Sheets("Holdings").Select

Columns("A:C").Select

Selection.Find(What:=Search, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

Exit Sub

ErrorCatch:
MsgBox "Search item not found"

End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I'd write that code slightly differently myself as there is no need to select the cells to perform the search.
Here's my version,

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim strFind     As String
    Dim rngFind     As Range
    
    Cancel = True
    strFind = Target.Value
    
    Set rngFind = ThisWorkbook.Worksheets("Holdings").Range( _
                    "A:C").Find(What:=strFind, _
                                LookIn:=xlValues, _
                                LookAt:=xlPart, _
                                MatchCase:=False)
        
        
    If rngFind Is Nothing Then
        MsgBox "Search item not found"
    Else
        'If found select the cell containing the term.
        rngFind.Parent.Activate
        rngFind.Select
    End If
End Sub

HTH!
 
Upvote 0
Hope that helps huh? haha

More than helps, thanks a ton! Understanding exactly what VBA code does it a little like reading Spanish for me. I know it sorta, but mostly I just parse my way through procedures I need to execute. it was getting quite aggravating, thanks so much for taking the time to help!
 
Upvote 0

Forum statistics

Threads
1,213,522
Messages
6,114,112
Members
448,549
Latest member
brianhfield

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