Auto fill cell color for row

thewiseguy

Well-known Member
Joined
May 23, 2005
Messages
1,019
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am trying to auto-fill cell color based on whether the value in column A is odd or even. Odd values would make the entire row (A-O) have no fill, and even values would make the entire row (A-O) have grey fill.

Can anyone provide any guidance?

Much appreciated.



/M
 
this worked as well
Just a few comments about that code, admittedly not knowing much about how your sheet will be used.

- If a text value happens to be entered in column A (like cell A6) in my sample, the code will error.
- If all the other values as shown in my sample are entered individually, then the colour will get applied correctly. However, assuming nothing else has been entered below A10, if you then delete that zero from cell A10, the colour will not be removed from row 10 even though that row no longer holds an even number.
- If multiple cells are altered at once (in most worksheets it is not uncommon for several cells to be selected & deleted, or for several values to be pasted or entered at once with Ctrl+Enter & in any of those circumstances the code will not update the colour for those rows.

All of the above can be overcome if they are, or become, an issue for you and you would prefer to use vba over Conditional Formatting.

I also point out that the Conditional Formatting that I suggested is not fool-proof either as a user might copy some data from elsewhere and paste it into column A (& possibly other columns too). That would destroy any Conditional Formatting that had been set up in those cells.

In summary, I am not championing either method as they both have potential drawbacks so you just need to choose the one that best suits your circumstances knowing those possible issues. :)
 
Last edited:
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
.. it's just that the 'CountLarge > 1' part has me confused.

If there is a link I could go to for more information, I'd appreciate it if you could let me know.
Using CountLarge in this code is just a very conservative precaution in case the user has changed more than 2,147,483,647 cells all at once. :biggrin:
If that had happened, using just Target.Cells.Count would cause an overflow error.
Ref: https://docs.microsoft.com/en-us/office/vba/api/excel.range.countlarge


But we do not want the script to activate if the user for example selected 1000 cells and tried to change them all at once we do not want the script to run. Like if the user tried to select 1000 cells and tried to clear them we would not want the script to run.
For the record, I disagree strongly with that. If the user changed 1000 cells in column A all at once, I would want the code to check them all and apply or remove the colour from each of those rows depending on the new value in column A.
 
Last edited:
Upvote 0
Just a few comments about that code, admittedly not knowing much about how your sheet will be used.

- If a text value happens to be entered in column A (like cell A6) in my sample, the code will error.
- If all the other values as shown in my sample are entered individually, then the colour will get applied correctly. However, assuming nothing else has been entered below A10, if you then delete that zero from cell A10, the colour will not be removed from row 10 even though that row no longer holds an even number.
- If multiple cells are altered at once (in most worksheets it is not uncommon for several cells to be selected & deleted, or for several values to be pasted or entered at once with Ctrl+Enter & in any of those circumstances the code will not update the colour for those rows.

All of the above can be overcome if they are, or become, an issue for you and you would prefer to use vba over Conditional Formatting.
Just so the OP has it if needed, here is what I believe to be a Change event procedure which addresses all of the concerns you raised above...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Cell As Range
  On Error GoTo NotColumnA
  For Each Cell In Intersect(Intersect(Target, Columns("A")), ActiveSheet.UsedRange)
    With Intersect(Cell.EntireRow, Columns("A:O")).Interior
      If Not Cell.Value Like "*[!0-9]*" And Cell.Value Like "*[02468]" Then
        .ColorIndex = 48
      Else
        .ColorIndex = xlColorIndexNone
      End If
    End With
  Next
NotColumnA:
End Sub
[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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