Question

VBA

Trying to have code search through column containing phrase BEN, then FH, then MH, and color cells with those phrases depending on which phrase is in the cell. I tried it two different ways and neither worked

For r = 2 TO 85 If InStr (cell.Value, BEN) Then Cells(r, 2).Interior. Color = vbRed ElseIf InStr (cell.Value, FH) Then CellsE6 X MONDO А в 1 Pt_Num Name 1 BEN1 2 BEN2 3 BEN3 4 BENA 5 BENS 6 BEN6 7 BENZ 8 BENS 9 BENE 10 BEN10 11 BEN11 13 12 BEN12 с L

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Steps to implement the Macro:

Select the table in which you want to use the conditional formatting.

Then Click the Alt + F11. The below page will open.

Microsoft Visual Basic - Book2.xlsx - [Sheetl (Code) Type a question for help File Edit View Insert Format Debug Run Iools Ad

In the page you have to select worksheet and selection range and paste the below code.

VBA Code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim My_Range As Range

Set My_Range = Worksheets("Sheet1").Range("B2:B85")

For Each Cell In My_Range

If InStr(Cell.Value, "BEN") Then
Cell.Interior.Color = vbRed
ElseIf InStr(Cell.Value, "FH") Then
Cell.Interior.Color = vbBlue
ElseIf InStr(Cell.Value, "MH") Then
Cell.Interior.Color = vbYellow

End If

Next
End Sub

Explanation

In Dim My_Range As Range we create a new variable named My_Range for specifying the cell range in which we want to apply the cell range in which we want to apply the rules of formatting.

Set My_Range = Worksheets("Sheet1").Range("B2:B85") define the cell range from B2 to B85. Within the range we are going to apply the rules hence we have used the for loop for that. Cell.Value define the value of the specific cell and Interior.color mention the color in which we want to apply on the specific cell. Instead of that we can also use the Cell.Interior.ColorIndex = 1 to mention the color based on the value of the right hand side.

Add a comment
Know the answer?
Add Answer to:
VBA Trying to have code search through column containing phrase BEN, then FH, then MH, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT