If cell Then multiple properties

ThomasOES

Board Regular
Joined
Aug 29, 2017
Messages
174
I use the code below to check the quality of a range of cells. If a quality is met I want the cell being checked to have a black background and white text. I can easily apply the code for a black background but I cant figure out how to add the property to have a white font. Do I need to repeat the entire If statement with Then cell.Font.Color = vbWhite? Or is there a more elegant way to apply multiple properties to a cell?

>Range("repsamps").Select
>For Each cell In Selection
>'IF SAMPLE IS HOMOGENOUS AT 95% THEN BLACK CELL WHITE TEXT
>If Abs(Application.Average(Range("repsamps")) - cell.Value) < _
>(Range("Lot95WVal") / 2) _
>Or Abs(Application.Average(Range("repsamps")) - cell.Value) = _
>(Range("Lot95WVal") / 2) _
>Then cell.Interior.Color = vbBlack

What I try and doesn't work is:

If Abs(Application.Average(Range("repsamps")) - cell.Value) < _
>(Range("Lot95WVal") / 2) _
>Or Abs(Application.Average(Range("repsamps")) - cell.Value) = _
>(Range("Lot95WVal") / 2) _
>Then cell.Interior.Color = vbBlack
>cell.Font.Color = vbWhite

>Next cell

It just colors all cells text white, doesn't apply the if parameters

Any help would be much appreciated.

Thanks

Tom
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try
Code:
If Abs(Application.Average(Range("repsamps")) - cell.Value) <= (Range("Lot95WVal") / 2) Then
   cell.Interior.Color = vbBlack
Else
   cell.Font.Color = vbWhite
End If
 
Upvote 0
I'm not going to take all the ">" out of what you have just to copy the whole thing, but you can use "If..... <=....." and skip the "OR". If you want to change more than one thing, you may have to use a multi line IF statement.

Code:
If ........... <= cell.Value Then
    cell.Interior.Color = vbBlack
    cell.Font.Color = vbWhite
End If
 
Upvote 0
Misread the OP, my code should be
Code:
If Abs(Application.Average(Range("repsamps")) - cell.Value) <= (Range("Lot95WVal") / 2) Then
   cell.Interior.Color = vbBlack
   cell.Font.Color = vbWhite
End If
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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