I was needing a way to format alternating pairs of rows in a worksheet with the same background color. When looking around I found the below link:
Excel VBA: Alternate Row Colors in Range
My problem is similar except that instead of wanting every other row auto filled with a background color I need adjacent pairs of rows colored. For instance, from my start point at row 4 columns A:T would be filled, row 5 columns A:T would have the same background color, rows 6 and 7 columns A:T would be without background color, rows 8 and 9 would share the background color as rows 4 and 5, repeated until the end of the spreadsheet.
I've made an attempt at using conditional formatting for this purpose but 1) I had not been able to get the background alternating for every pair of rows from my start point and 2)it overrides the few special cases that have a different background color 3) conditional formatting does not allow me to manually format any rows that the conditional formatting function formatted.
Many thanks to the commenters for their suggestions (which put me on the right track) but due to the limitations of Conditional Formatting I cobbled together the following Macro that allows the background to be formatted to my needs without eliminating the ability to correct for special cases. The code is heavily commented to help other newbies to understand what the code means and what to modify to change the behavior of the Macro.
Sub AlternateRowColors()
''PURPOSE: To format the background color of alternating pairs of rows in a designated range of cells with values.
'' A correction to account for a possible empty row at the end of the range not having a value failing to follow the
'' desired pattern is included.
'' Example: Column A
'' Row 1: Green
'' Row 2: Green
'' Row 3: No Background Color
'' Row 3: No Background Color
'' Repeating this pattern until the end of the used cells of a worksheet.
Dim lastUsedRow As Long ''Variable to hold the last row number.
lastUsedRow = Range("A200").End(xlUp).Row ''This checks backwards from cell A200 to A1 for the last row with an entry in Column A
''and saves the row number in variable lastUsedRow. Modify this as needed for each worksheet.
If lastUsedRow Mod 2 = 0 Then ''This block of code corrects for the second row of an entry not being highlighted at the
lastUsedRow = lastUsedRow + 1 ''end of the worksheet if the first cell in the following row is blank.
Else
End If
For Each cell In Range("A4:T" & lastUsedRow) ''Sets the range of rows and columns that the background color formatting is to affect.
If cell.Row Mod 4 = 0 Then ''Highlight row if the row number has a divided remainder of zero
cell.Interior.ColorIndex = 35 ''Sets background color by using .ColorIndex instead of RGB. Change to suit your need.
Else
If cell.Row Mod 4 = 1 Then ''Highlight row if the row number has a divided remainder of 1
cell.Interior.ColorIndex = 35 ''Sets background color by using .ColorIndex instead of RGB. Change to suit your need.
End If
End If
Next cell
End Sub
''ADDITIONAL NOTES:
''NONE
Try these formulas in conditional formatting > using a formula to determine which cell's to format:
=AND(ROW()>3,MOD(ROW(),4)=1)
and
=MOD(ROW(),4)=0
both would apply to $A:$T
Put your rules for specially formatted cells after these general rules.
Hope this helps.
In conditional formatting use the formula
=Mod(Row(),4) < 2
in the cells you want the rules to apply to
Related
I've been having issues conditionally formatting a sheet 2 (Shipping Request List) that is mirroring select columns of sheet 1 (Master List).
-Starting in sheet 2 (Shipping Request List), column B, cell B2, has ='Master List'!D2 (from sheet 1) and continues down for hundreds of rows.
-The data in this field is mirroring cell 'Master List'!D2 (sheet 1) which could either be "Open" (or a number of other items based off a drop down list... no formulas). I'm only interested in what happens when the cell says, "Open".
-Cell D2 in sheet 1 has the following conditional formats based on D2 saying "Open" and a date in cell V2 in the Master List sheet.
=AND($V2<TODAY(),$D2="open") [turns purple]
=AND($V2-TODAY()>=0, $V2-TODAY()<=2,$D2="open") [turns red]
=AND($V2-TODAY()>=3, $V2-TODAY()<=4,$D2="open") [turns orange]
=AND($V2-TODAY()>=5, $V2-TODAY()<=7,$D2="open") [turns yellow]
When I try conditional formatting based on the values in sheet 2, i.e. by the value "Open" mirrored from sheet 1, the colors are not correct.
I tried various approaches.
Approach 1:
Change the conditional format equations in sheet 2 to reference the cell in sheet 1 instead of directly referencing the cells in sheet 2.
Approach 2:
Try using INDIRECT in the conditional format equations in sheet 2...but I'm not sure I did this correctly.
Approach 3:
Use a VBA code to create a UDF on sheet 1 to extract the color code of column D into an adjacent column (C), then filter by that number for the codes that represent purple, red, orange, and yellow.
(I am not at all good with VBA, but I can copy and paste a module and follow instructions.)
Function FindColor(n As Range) As Integer
FindColor = n.Interior.ColorIndex
End Function
In column C, C2 had =findcolor(D2) and then that got pulled down. This approach gave me numbers, but it gave me the same number for purple, blank, etc... extremely inconsistent.
Approach 4:
In new column C, add a formula to make the words either "purple", "red", "orange", or "yellow" appear based on the conditions mentioned in the conditional formatting formulas. I got only so far as the formula started getting a little too complicated for me to get through it. Only got this far (didn't even add orange and yellow yet) but the formula below is not returning "red" when the conditions call for it:
=IF(AND($V662<TODAY(),$D662="open"),"purple",IF(AND($V662-TODAY()>=0,$V662-TODAY()<=2,$V662="open"),"red",""))
I think I was getting close on the last approach, but I realize it's a lot of formula and will be added to 700+ rows, which will continue to grow.
As a slightly different approach: you could use a UDF to return the "color" for each row, and then run your conditional formatting rules based on the return values.
Put this in a regular module:
Function Classify(theDate, theStatus) As String
If theStatus = "open" Then 'check status
'If theStatus = "open" Or theStatus = "issue" Then
If Len(theDate) > 0 Then 'has a date?
Select Case theDate - Date
Case Is < 0: Classify = "purple"
Case Is >= 5: Classify = "yellow"
Case Is >= 3: Classify = "orange"
Case Is >= 0: Classify = "red"
End Select
End If
End If
End Function
In your worksheet you'd use (eg)
=Classify($V662, $D662)
I need to add values of a range when its contents are of a certain font color (e.g., black). What I have is a a table where I am conditionally formatting the color of values. For e.g., if the "Status =Carry Over" then I am coloring the row Red. (see attachment).
Now, after the conditional formatting, I want to sum all the number values under a specific column that are NOT in red color.
I have a piece of vba code to add such values, but the problem is, the conditional coloring is throwing the code off. As long as I am manually coloring the rows, the code is ignoring the red rows. If I use the conditional coloring option, then even the colored rows are taken into account.
I am calling the below UDF using this formula =ConditionalColorSum(C2:C30)
Public Function ConditionalColorSum(rnge As Range) As Double
' Total only cells with red font numbers
Application.Volatile
Dim Total As Double, cl As Range
Total = 0
For Each cl In rnge.Cells
If cl.Font.Color = vbRed Then 'Change 'vbRed' to the color you want
Total = Total + cl.Value
End If
Next
ConditionalColorSum = Total
End Function
The Conditional formatting formula looks like above:
As per THIS
Actions such as changing the conditional formatting or table style of a range can cause what is displayed in the current user interface to be inconsistent with the values in the corresponding properties of the Range object. Use the properties of the DisplayFormat object to return the values as they are displayed in the current user interface.
The only way to get the color due to conditional formatting is to use DisplayFormat
If cl.DisplayFormat.Font.Color = vbRed Then
But per THIS
Note that the DisplayFormat property does not work in user-defined functions.
So one cannot use UDF to count the colors directly from the sheet. One can use a SUB but it would be easier just to use the same criteria that the custom format uses to count:
=COUNTIF(E2:E30,"Carry Over")
or to count where it is not red:
=COUNTIF(E2:E30,"<>Carry Over")
I'm working on data-analysis where i would like to be able to automatize color fill when looking through large amount of data where there are abundant amount of ghost logs and taking too much of my time as they are severely irrelevant.
So what i would like to do in Excel, is to be able to color fill a cell when the number changes in a column marking a different set of logs.
For instance if there are six rows of log number 456455, i would like the code to color fill the first cell when the number changes to 456456 so that it helps me identify logs faster when i know where the sets are starting. I'm kind of a newbie when it comes to Excel but this loop would help me a lot!
Thx for your time
This can be done with conditional formatting. Use a rule that compares the current cell with the cell in the row above and format if the two are different. Note that you will need to use relative references without $ signs. In the screenshot below, the conditional format is applied from row 2 to 19 and in row 2 the formula compares A2 with A1, in the next row it compares A3 with A2, and so on. If the two cells are different, the cell will change colour.
If you have some knowledge in VBA, you can implement a macro that looks at the column where you have your log number and if the value changes from one cell to another, then you highlight this cell.
I attached a template of code that works for this task.
Sub highlightChange()
Dim preVal As Integer
preVal = 0
For Each o In Range("A:A")
'Go through column
If o.Value <> preVal Then
o.Interior.Color = vbRed 'Color the selection
End If
preVal = o.Value
Next o
End Sub
There may be other solution without VBA, however, it is quite easy and practical to use a macro.
I am building a residential stacking plan in which each cell = unit type with specific color given conditional formatting.
2 tables follow below for each unit reflect a sqm size and a $ value.
I need to reflect only the cells' color into the following tables.
I need a dynamic solution and would prefer avoiding vba (since I'm not proficient), but will use if necessary. Thanks in advance!
Find Image HERE
Set up your Conditional formatting as normal on the first table like this:
Note my table starts at cell C4 but yours is in a different place and should be adjusted accordingly. make sure you DON'T have the $ symbol on the formula in the rule but you do have it on the 'Applies to' section
Now copy and paste this formatting onto the second table.
Finally edit the formulas in the conditional formatting so that they point to the starting cell of the FIRST table. It should look like this:
Note that the formatting 'Applies to' the second table but refers in the Formula to the values in the first table.
The result is this:
You can repeat this for other tables if you need to.
As you are working in Excel 2003(!), follow the following steps:
Select the cells in the second table.
In the menu, choose Format - Conditional Formatting.
In the Conditional Formatting box, choose Formula Is.
In the text box, enter the cell reference of the FIRST table (eg C4="4+"), do not enter any $ symbols.
Click the Format button and select the background fill to match the one in the first table.
Add the other conditions in the same way by clicking the Add>> button.
If you change the color code values (B21:B26) from 2 rooms to 2 (to match your second table), the following should do the trick. Basically, this code is not using conditional formating. Getting the color from conditional formating can be somewhat laborious and tricky (google "excel vba find color conditional formatting"). Instead, the present code reads the color in your Color Code cells, and apply it to the other two ranges.
Private Sub BckgndColor()
Dim ColorCodeRange As Range
Dim NoOfRooms As Range
Dim CellColorIndex As Integer
Dim c As Range
Dim d As Object
Set ColorCodeRange = Worksheets("Sheet1").Range("B21:B26")
Set d = CreateObject("scripting.dictionary")
'Add the pairs (value, color) to dictionary
For Each c In ColorCodeRange.Cells
d.Add c.Value, c.Interior.ColorIndex
Next
Set NoOfRooms = Worksheets("Sheet1").Range("M25:V36") 'Here the range of Table 2 (M25:V36 in your example)
'Scan range, and assign color
For Each c In NoOfRooms.Cells
If d.Exists(c.Value) Then
c.Interior.ColorIndex = d(c.Value)
c.Offset(16, 0).Interior.ColorIndex = d(c.Value) 'If Table 3 is always 16 rows down, this shoud work
End If
Next
Set d = Nothing
End Sub
I gave the option data validation list in sheet 2, while I selecting the option by list the cells will change . for that cells I want to get color also from source table in sheet1 to sheet2 .
The conditional formatting options works well to identify duplicates. How could I use the detected duplicates to write a value to a separate column? 1 for duplicate, 0 for not a duplicate. I thought I could use a VBA function based on the cell colour. Excel however does not store the dup detected cell color in the normal cell color property.
Note: 99 is not the light red color number, it's just for reference.
Function LightRed(rng As Range) As Boolean
If rng.Interior.ColorIndex = 99 Then
LightRed = 0
Else
LightRed = 1
End If
End Function
As tigeravatar said, you can use
=--(COUNTIF(A:A,A1)>1)
to detect duplicates and then you can apply a condition formatting on Column B where if cell value = 1, change background color to red