If more than cell in a range meets or exceeds a criteria then only one response

ThomasOES

Board Regular
Joined
Aug 29, 2017
Messages
174
Hello,
I use the code below to check a range of cells. If true then add "X99%" to text in cell "repsym". Problem is, sometimes the range has more than one cell that returns a true value. Then cell "repsym" has several "X99%" added to text.
How to check a range, and if one or several return a true value, then only one response.
Thanks
Tom

Code:
For Each cell In Range("GraphSampAvg").Cells
    If cell.Value <= (Range("Lot99WVal") / 2) Then _
    Range("repsym").Select
        With Selection
        .Value = Range("repsym") & " X99%"
        .Interior.Color = vbBlack
        .Font.Color = vbWhite
        End With
Next

Strangely, I would get error code "End if without if". I removed the end if and it works albeit incorrectly with all the extra "X99%".
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try:

Code:
For Each cell In Range("GraphSampAvg").Cells
    If cell.Value <= (Range("Lot99WVal") / 2) Then 
        With Range("repsym")
           .Value = .Value & " X99%"
           .Interior.Color = vbBlack
           .Font.Color = vbWhite
        End With
        Exit For
    End If
Next
Note I removed the _ from the If line and replaced the End If.
 
Upvote 0
Double Thanks Eric
Code is nice and all my future code will go straight to With the range instead of the clumsy select then With selection.
Thanks
Tom
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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