vba to select rows in one sheet based on a cell value in another sheet

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
683
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I'm looking for some vba that will take the value in the selected cell (variable) in Sheet1 and then select all the rows in Sheet2 that contain exactly the same value in Column J Sheet2. So if Sheet1 cell N23 is selected and it contains the words 'tree stump' the vba will select all the rows in Sheet2 that have the words 'tree stump' in Column J. Would it also then be possible for the uppermost selected row in Sheet2 to be at the top of the display on the screen?

Any help much appreciated.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Untested. Note the comment regarding the lookat argument in the range.find method.
VBA Code:
Sub SelectIfMatch()
'run this code from Sheet1
Dim Sh1 As Worksheet, Sh2 As Worksheet, Lookfor As Variant, Fnd As Variant, R As Range, fAdr As String
Set Sh1 = Sheets("Sheet1"): Set Sh2 = Sheets("Sheet2")
Lookfor = Selection.Value
'on next line, change lookat to xlWhole to find only those cells that contain the entire string
'in the activecell on Sheet1
Set Fnd = Sh2.Range("J:J").Find(what:=Lookfor, LookIn:=xlValues, lookat:=xlPart, MatchCase:=False)
If Not Fnd Is Nothing Then
    fAdr = Fnd.Address
    Do
        If R Is Nothing Then
            Set R = Fnd
        Else
            Set R = Union(R, Fnd)
        End If
        Set Fnd = Sh2.Range("J:J").FindNext(Fnd)
        If Fnd Is Nothing Then Exit Do
        If Fnd.Address = fAdr Then Exit Do
    Loop
End If
If Not R Is Nothing Then
    Sh2.Activate
    R.Select
Else
    MsgBox "Value of selected cell on Sheet1 is not found in col J of Sheet2"
End If
End Sub
 
Upvote 0
Solution
Untested. Note the comment regarding the lookat argument in the range.find method.
VBA Code:
Sub SelectIfMatch()
'run this code from Sheet1
Dim Sh1 As Worksheet, Sh2 As Worksheet, Lookfor As Variant, Fnd As Variant, R As Range, fAdr As String
Set Sh1 = Sheets("Sheet1"): Set Sh2 = Sheets("Sheet2")
Lookfor = Selection.Value
'on next line, change lookat to xlWhole to find only those cells that contain the entire string
'in the activecell on Sheet1
Set Fnd = Sh2.Range("J:J").Find(what:=Lookfor, LookIn:=xlValues, lookat:=xlPart, MatchCase:=False)
If Not Fnd Is Nothing Then
    fAdr = Fnd.Address
    Do
        If R Is Nothing Then
            Set R = Fnd
        Else
            Set R = Union(R, Fnd)
        End If
        Set Fnd = Sh2.Range("J:J").FindNext(Fnd)
        If Fnd Is Nothing Then Exit Do
        If Fnd.Address = fAdr Then Exit Do
    Loop
End If
If Not R Is Nothing Then
    Sh2.Activate
    R.Select
Else
    MsgBox "Value of selected cell on Sheet1 is not found in col J of Sheet2"
End If
End Sub
Hi,
Thanks very much for this code and your time - it works great and highlights the cells that contain the value but is it possible to highlight the complete rows?
It's not the end of the world if it can't be done, it would just save me a bit if extra time.
Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,271
Messages
6,177,589
Members
452,784
Latest member
talippo

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