VBA For each for selected cells given criteria

jakobt

Active Member
Joined
May 31, 2010
Messages
337
In a range of selected cells I want to colour each row d:f yellow if the following criteria is met.

The criteria is that the cell in the selected cell should contain the text "jakob".

Also I want to write this code using a for each statement for practice reasons.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You said:
each row d:f yellow

We have no rows d:f

We have rows like 2 3 10
We have columns like A B C
 
Upvote 0
Maybe this is what you want:
Will search selection for Jakob
Code:
Sub My_Selection()
'Modified  7/18/2018  6:52:51 PM  EDT
Dim r As Range
For Each r In Selection
    If r.Value = "jakob" Then r.Interior.Color = vbYellow
Next
End Sub
 
Upvote 0
Thanks.
It worked to a large extent.
What I meant say my selected cells are in column B, if the value in column B is Jakob, the cells in the same row from Column D:F should be coloured yellow.
 
Last edited:
Upvote 0
Well I assume by selected you mean cells in column B
Try this:
The script looks for jakob in column B and if found columns D:F are colored Yellow
Not sure why your saying selected cells.

Code:
Sub My_Selection()
'Modified  7/18/2018  7:20:14 PM  EDT
Dim r As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
For Each r In Range("B1:B" & Lastrow)
    If r.Value = "jakob" Then r.Offset(, 2).Resize(, 3).Interior.Color = vbYellow
Next
End Sub
 
Upvote 0
If you only want selected cells in column B
Try this:
Code:
Sub My_Selection()
'Modified  7/18/2018  7:27:46 PM  EDT
Dim r As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
For Each r In Selection
    If r.Value = "jakob" Then r.Offset(, 2).Resize(, 3).Interior.Color = vbYellow
Next
End Sub
 
Last edited:
Upvote 0
If your selection will not always be in Column B
Try this
Code:
Sub My_Selection()
'Modified  7/18/2018  7:56:35 PM  EDT
Dim c As Range
Dim r As Long
For Each c In Selection
    If c.Value = "jakob" Then
        r = c.Row
        Cells(r, "D").Resize(, 3).Interior.Color = vbYellow
    End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,397
Members
448,957
Latest member
Hat4Life

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