Help with VBA Code

Regtompj

New Member
Joined
Jul 4, 2010
Messages
37
I have a VBA code that highlights a name all over the sheet when double clicked. I am currently using it on a worksheet that has some similar names so it's highlighting multiple areas. Is there a way to have it find the exact match and only highlight that?:)
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Can you please post your existing code?
 
Upvote 0
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    Dim cell As Range
    
    Application.ScreenUpdating = False
    
    For Each cell In ActiveSheet.UsedRange
        If (cell = Target) And Not IsEmpty(cell) Then cell.Interior.Color = vbYellow
    Next
    
    Cancel = True
    
End Sub
 
Last edited:
Upvote 0
Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim c As Range
Dim FirstAddress As String
Dim wksh As Worksheet
Static ColIdx
Cancel = True
If IsEmpty(ColIdx) Then ColIdx = 33 Else ColIdx = ColIdx + 1
If ColIdx > 43 Then ColIdx = 33
ActiveCell.Interior.ColorIndex = ColIdx
For Each wksh In ThisWorkbook.Worksheets
With wksh.Range("A5:R95")
Set c = .Find(ActiveCell.Value, LookIn:=xlValues)
If Not c Is Nothing Then
FirstAddress = c.Address
Do
c.Interior.ColorIndex = ColIdx
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
End If
End With
Next wksh
End Sub[CODE]
 
Upvote 0
Try:

Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim c As Range
Dim FirstAddress As String
Dim wksh As Worksheet
Static ColIdx
Cancel = True
If IsEmpty(ColIdx) Then ColIdx = 33 Else ColIdx = ColIdx + 1
If ColIdx > 43 Then ColIdx = 33
ActiveCell.Interior.ColorIndex = ColIdx
For Each wksh In ThisWorkbook.Worksheets
    With wksh.Range("A5:R95")
        Set c = .Find(ActiveCell.Value, LookIn:=xlValues, [B][COLOR=red]Lookat:=xlWhole[/COLOR][/B])
        If Not c Is Nothing Then
            FirstAddress = c.Address
            Do
                c.Interior.ColorIndex = ColIdx
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> FirstAddress
        End If
    End With
Next wksh
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,282
Members
452,902
Latest member
Knuddeluff

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