Highlighting cell if one date is after another? - excel

I have a couple columns of data, and I am trying to highlight those instances where the second date occurs before the first, to show if there are any mistakes in the data.
ID Start Date End Date
1 01/01/15 01/15/15
2 01/06/15 01/02/15
3 01/06/15 01/03/15
4 01/11/15 01/13/15
I would like to be able to highlight the cells where the end date is earlier than the start date. All of the date conditional formatting links I have been directed to are for basing it off of current date, or one specific date, and I am kind of lost.
Thanks!

This is kinda complicated if you have no coding experience. The best way to solve this problem is to write a VBA script yourself. So I will go Step by step with the process of doing that with the complete code.
1) Open your Excel file, then go to the Developer Tab.
(If you do not see it, go to File->Options, Click on Customize Ribbon, and add click on Developer for the main tabs.
2) Click on the Visual Basic button, under the developer tab.
3a) Paste the following code in:
Sub ColorCellsWithIncorrectEndDate()
Dim Rows
Rows = ActiveSheet.UsedRange.Rows.Count
For i = 2 To Rows
If Cells(i, 2).Value > Cells(i, 3).Value Then
Cells(i, 3).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next
End Sub
3b) The code above works if your excel sheet is in the exact same format it was listed above.
--If the cells are not in the same columns, then you will adjust the numbers that are in "Cells(i,#)" so that the number corresponds to the column. (ie. A=1, B=2, C=3...). Right now it is checking for column B (Start Date) to be greater than column C (End Date). And is then coloring Cell C if it is true.
--If the cells are not in the right row then you adjust the line that says "For i = # To Rows" The Number corresponds directly to the row number.
--If you want a different color then go to this website: http://www.w3schools.com/tags/ref_colorpicker.asp, and replace the "65535" with any one of those valid numbers.
4) SAVE and exit out of visual basic
5) Return to your Excel Sheet and click Developer->Macros. (A dialog box will appear)
6) Select "ColorCellsWithIncorrectEndDate" in the dialog box, and click Run.
7) All the end dates that are past the start date will be highlighted in yellow (or whatever color you chose)
Hope this helps!
-BlindingFog

Dates can be compared by < just like numbers. One possibility would be to research how to do conditional formatting which highlights the smallest entry in each row. If any row has the second rather than the first date column highlighted then it is a problem row.
It is also possible to do it more directly. The key thing to do when none of the predefined conditional rules work is to use Conditional Formatting/New Rule/Use a formula to determine which cells to format. If the data that you show starts in A1 (so that 01/15/15 is in C2), go to C2 and create a conditional formatting rule given by the formula
= C2 < B2
Then -- just paste the formatting down the rest of the dates in column C

Related

VBA Conditional Format .rank value based on a cell - Top10

I originally needed to select top 5% of the selected items but, it couldn't roundUP the number of highlighted items. For example: If there are 25 items in the list, 5% is 1.25 and Conditional Format is only selecting 1 item. According to my report, it should round it up and select 2 items.
As I couldn't find a way to do this, I decided to manually calculate the number of items which would show how many to be highlighted (in a cell).
It would be easier if there is a way to make the conditional format round up the number of selected items.
such as:
.rank = 5 (but should round it up)
.percent = True
If it is not possible,
I would like to highlight the top X values in a column using the conditional formatting.
The value X (The rank) is calculated in a cell and will change everytime the macro is run.
I've tried the code below but, it doesn't work.
Dim i As Integer
i = Workbooks("a.xlsm").Worksheets("b").Range("A1")
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Top
.Rank = i
.Percent = False
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
I'm getting the error on ".rank = i" line which makes me think it is not possible to assign a variable there or I might be assigning the cell wrongly.
I also tried ".rank = i.Value" which seems to be wrong as well (Compile error: Invalid qualifier)
I'm a newbie in vba and any help would be appreciated.
ps: using office 365.
Thank you
Update: The code above works as long as the "i" value is not zero.
I managed to create a simple If statement to prevent the error.
'In my report i cannot be below 0
If i > 0 Then
Insert the code above here
'If there is no data to be highlighted
Else
'Do Nothing
End If
If a Non-VBA solution can work for you, you can use this. I made a fake dataset, just a list of 25 values (numbers 1 to 25). You want to highlight 5% higher values. 5% of 25 = 1,25, but rounded up it's 2. So you want to highlight any cell which value is one of those top 2 values in this case.
I got this:
As you can see, values 25 and 24 are highlighted. The Conditional Formatting Rule (CFR) I'm using is based on this formula:
=RANK(A1;$A$1:$A$25;0)<=ROUNDUP(COUNTA($A$1:$A$25)*0,05;0)
This is how it works:
RANK(A1;$A$1:$A$25;0) will rank the value inside the list, in descendent order.
ROUNDUP(COUNTA($A$1:$A$25)*0,05;0) will count how many cells are in the list (25) and will get the 5% (multiplying by 0,05, change 0,05 to the % you need), and it will round it up (in this case, the output will be 2).
Last Step will compare the value of Step 1 with the value of step 2. If it's less or equal to 2 (in this case), it will be highlighted as you can see
Please, note that this CFR may not work properly if there are blanks values.
Also, the good thing is that changing the 0,05 part to whatever % you need, the CFR will update perfectly. You can even bind it to a cell, and change the value of the cell manually :)
Hope this works for you!
AppliesTo property gives you the range where the condition applies.
You can then get the cell count from that, multiply it with the 5% and round it up using a WorksheetFunction.
.Rank = WorksheetFunction.RoundUp(.AppliesTo.Cells.Count * 0.05, 0)
.Percent = False

How to dynamically access column properties in a With Range statement for conditional formatting

I am trying to write a script to take an easy look into my data. The data is structured as follows:
Row 1: Parameter Name
Row 2: Lower Limit
Row 3: Upper Limit
Row 4: Unit
Row 5 and below: data (can go up to a couple thousands lines and couple hundred columns).
The thing I want to achieve is a script which formats each cell from row 5 down, to color green if it is in between the limits, and to color red if it is not. Each cell should look to it's own column row 2 and 3 for the limits.
I have tried going cell per cell, or column by column. Both worked fine on smaller datasets, but showed problems (excel freezing and eventually closing) on bigger datasets.
I am now trying to format a complete range (because excel has no problems when I do a big range in one piece by hand whatsoever) at once, but I can't access the individual column properties.
The code I am using:
With formatRange
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, Formula1:="=" & Cells(2, formatRange.Column).Address, Formula2:="=" & Cells(3, formatRange.Column).Address
.FormatConditions(1).Interior.Color = RGB(0, 249, 49)
End With
Now say my range is from A5:B10.
formatRange will be A5:B10.
I would expect every cell from A5:A10 to compare their values against A2 and A3. This is indeed the case
But the cells of B5:B10 also compare their values against A2 and A3.
So my question is, is there a scalable way I can make range B5:B10 look at B2 and B3 instead?
Edit
The answer of #Ryan B. is an easy and correct way of doing it by hand.
The problem in vba turned out to be the following:
Formula1:="=" & Cells(2, formatRange.Column).Address would ultimately result in Formula1:="=$A$2"
As suggested by the accepted answer, this needed to change to Formula1:="=A$2" to work.
The solution I found was to create a function which cut of the first character, and create the correct formula this way.
This can be done by tricking Excel's absolute and relative referencing within conditional formulas. No VBA is going to be required. Here is a quick mock of how I understand your data:
Select the first cell of your 'Row 5' range -- where you're going to apply the conditional formulas.
Open the conditional formatting dialog from the Styles button group in the Home Ribbon (this is B5 in my mock-up),
Create a 'New Rule'
Choose 'Use a formula to determine which cells to format' rule type
Begin typing the following rule. You will have to be quite careful and avoid all use of the arrow keys. If you need to get to a different point in the formula, use your mouse to move the insertion point. Make the proper adjustment in your formula if that initial column isn't column 'B' in your worksheet
=AND( B5 >= B$2, B5 < B$3)
Pay special notice to the Dollar Signs. There are NO absolute references used for the target cell, B5. There are absolute references (that's the dollar sign) in front of the row numbers for Lower and Upper, but not on the columns.
Set you're desired look for the "in-bounds" formatting and select OK.
Repeat the steps for your "out-of-bounds" formatting. Use the formula
=OR( B5 < B$2, B5 >= B$3)
Finally, to apply the formulas to your entire range:
Select the cell with the prepared formulas (B5 in this example) and hit [ctrl] + [c] to put excel into cut/copy mode
Select the entire target range
Right click and take 'Paste Special' from the context menu
Paste as formats
And your formatting should propagate through the worksheet.
Hope it helps. Always experiment on a copy of your workbook :)

Excel 2013 color fill when number changes in a column

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.

Excel: Using IF statement to determine if multiple columns in a row are highlighted

I want to know how I can use an IF statement (or another function) to determine if all the columns pictured below in a particular row are highlighted:
Screenshot of Table
I'm looking to put the function in Column T and return "Buy" if all columns within the row are highlighted.
Thanks in advance
Now I have tested it but not sure what green you use. Change it if I'm incorrrect.
Paste this in a vba module:
Function color(c As Range)
If c.Interior.color = 5296274 Then color = True
End Function
Then use it like:
=color(A1)
It will return true if A1 is green.
In your case you need to build a and like:
=and(color(A1), color(B1))
And so on...
And you need to save the workbook as macroaktivated workbook xlsm.
Edit added picture and other colorcode. This is the left green color of "standard colors".

Background formatting pairs of rows in a spreadsheet

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

Resources