How to fill cells based on complicated formula? - vba

I have a complicated formula. What I'm looking to do is to set it up where if column A is YR then column B-J will highlight red if the date is more than 2 years ago and yellow if the date is within 30 days of reaching the 2 year mark.
If column A is P1, P2, P3, P4, or P5 then column B-J will highlight red if the date is more than 1 year ago and yellow if its within 30 days of reaching the one year mark.

You will want to create two conditional formatting rules. First select B2:J6 (as laid out in the image below, the bottom row will vary with your own data) with B2 as the Active Cell. Create a new formula using the Use a formula to determine which cells to format and supply the following for Format values where this formula is true:
=AND(OR($A2="P1",$A2="P2",$A2="P3",$A2="P4",$A2="P5",$A2="YR"),B2<=EDATE(TODAY(),(1+($A2="YR"))*-12))
Click Format and supply a red Fill. I also added a white Font for readability. Click OK to accept the format and then OK again to create the new rule.
With B2:J6 still selected, repeat with the following formula for a yellow fill.
=AND(OR($A2="P1",$A2="P2",$A2="P3",$A2="P4",$A2="P5",$A2="YR"),B2>EDATE(TODAY(),(1+($A2="YR"))*-12),B2<=(EDATE(TODAY(),(1+($A2="YR"))*-12)+30))
You results should be similar to the following.
     

Related

Conditional Formatting (If/when)

Ok so I want a row to highlight red when the date in cell C1 is more than the cell in B1, but only if this is a difference of two months or less. I am sorry, I don't know how to embed a dummy spreadsheet. So for example, if B1 (clinic appointment due date) is Sep-17 and C1 (physical checks due date) is Oct-17, I want this row to highlight. If C1 is Dec-17, I don't want the row to highlight because this is more than two months from Sep-17.
Edited based on your new description, using Column A to represent Clinic Appointment Date and Column B to represent Physical Checks Due Date.
Use a rule based on DAYS().
Click on cell A1
Choose New rule from the Conditional Formatting menu.
Choose Use a formula to determine which cells to format
In Format values where this formula is true:, use the below formula:
=DAYS(B1, A1) < 60
Set up the formatting you want to use to highlight the cells. In the image below, I've set the ones that meet your requirements for highlighting to appear in red, and applied it to the first two rows in Column A (A1 and A2):
Here's the sheet with sample data in two rows, showing the conditional formatting working as requested:

Conditional formatting formula to highlight appropriate date in range

I need a formula for conditional formatting that will highlight a date between A2:Z2 which matches a number that I enter into a “Committed Sessions Cell” (A1). In row 2 there are a series of numbers that appear above each date column (1,2.3, etc). For example, if I enter a “3” in cell A1, the date in J3 should match the number 3 above it and be highlighted. The idea here is to provide a quick visual prompt for how many sessions are in a client’s contract.
Note: the sequenced numbers 1,2,3 etc in row 2 appear every 5th column (with nothing in between) but there IS other data in between the dates in row 3. Only the appropriate date should be highlighted.
A B C D E F G H I J K L M
1 3
2 1 2 3
3. 1/2/14 2/3/14 2/15/14
With grateful thanks,
~ Jay
Your example is not consistent. If you have the date every fifth column, the dates should be in columns A,F,K,P, etc. with 4 columns between 2 points.
I came up with the following formula: =AND(A2=$A$1,MOD(COLUMN(A3)-1,5)=0) which is applicable to the entire 3rd row. Create it as follow:
IMPORTANT: Select cell A3 (the reference point for the formula)
Without selecting another cell, highlight the entire row 3
Go to Conditional Formatting -> New rule
Choose "Use a formula to determine which cells to format"
In "Format values where this formula is true", put =AND(A2=$A$1,MOD(COLUMN(A3)-1,5)=0)
Choose the formatting that you want (example: Fill with yellow)
Click OK all the way
NOTES:
To change the location of your Committed Sessions, change $A$1 to another cell. Important to keep the dollar signs
The MOD function is the one that controls every fifth column. If you want the highlight every 4th column (i.e. 3 cells between each point such as A, E, I, etc.), replace the number 5 with the number 4

Summarize data in Excel with VBA

I have a table in Excel with dates and values. Every day there were a number of different values.
I want to summarize how many of each value there were every day.
Example:
From this
Date Value
10/1 Blue
10/1 Blue
10/1 Red
11/1 Blue
11/1 Blue
I want to get a new table with something like this:
10/1 11/1
Blue 2 2
Red 1 0
I'm convinced this is possible to do in VBA/Excel. Does anyone have any ideas?
Assuming the first table is in A1 and the second in E1, you could use a formula in F2 like:
=SUM(($A$2:$A$6=F$1)*($B$2:$B$6=$E2))
This is an array formula so you need to validate it by pressing CTRL+SHIFT+ENTER.
A2:A6 is the range with the dates, B2:B6 the range with the colors in your first table.
The first part of the formula says: Retain records where the value in column A is 10/1 ( = F1).
The second part says : Retain records where the value in column B is Blue ( = E2).
The '*' is equivalent to AND.
More about it here: Multiple conditions in excel 2002
The solution was, just as Alex K said, to use the Privot Table function included in Excel.
Thanks for your help!

Sum of unfixed number of cells if corresponding cell has given value

I am strugling how to make this happen. I have a worksheet where the data is going to be read in. The size of the data will vary from time to time. I have programmed a generated summation column after the data is read in. I want each cell to sum all values in the row, with the index value in the first row equal to the value in the first row in the summation column. The picture might give you a less abstract visualization of the case. I included the manual formulas for row 3 in row 12 as text. I want to do this in VBA. There might be up to 50 sets ([2010,2011,avvik] or [2010,avvik] or [2011,avvik]). There are two variables with a saved number (column number) for both the start and the end of the data area.
In other words; The money column under "SUM 2010" (which in my program actually is only 2010) should sum every cell in the given row, which has the value 2010 in row 1 in the same column. The same goes for 2011.
(You might want to save/open the picture for details)
You can do this with the formula SUMIF. For Q3, you would write:
=SUMIF(A1:P1, "2010", A3:P:3)
In layman's terms, you are saying, look at all the cells in the range A1 - P1 and for each one, if the value happens to be "2010", I want you to add the value in the range A3 - P3 to the sum.
BTW, you can also use this formula in cell Q11 to get the totals instead of the current formula you have. The fact that the year dates and numbers are in the same column make this really easy.

Displaying a timeline in crystal reports or excel

I have an application that keeps rent periods of a parking lot. I have a SQL database with this information:
idLot dateFrom dateTo
------------------------------------------
1 01/03/2011 30/07/2011
2 01/01/2011 30/05/2011
3 01/02/2011 30/07/2011
6 01/02/2011 30/06/2011
And I need to display the information like the image below.
1
I cannot achive the goal with Excel or Crystal Reports. I also use ComponentOne FlexGrid. Any ideas?
Thanks in advance.
It's not the prettiest option, but you can do this in Excel using the Sumproduct() function. In the screenshot below, the sumproduct goes through each line of your datatable and does 3 things (the selected cell is B17 by the way):
It determines whether each row has the idlot it is looking for (evaluates to true/false)
It determines whether DataFrom is <= the column of your display table (evaluates to true/false)
It determines whether DataTo is >= the column of your display table (evaluates to true/false).
This results in a 0 or 1 in each cell of your display table. This is close, but not exactly what you're looking for. I then added some conditional formatting:
If the result of the equation was 0, then the cell's font is white.
If the result of the equation was 1, then cell's font and background are both green.
When you apply the conditional formatting to the entire graph, you get the last 2 rows, which is green for occupied and white for unoccupied.
The only remaining problem is how to account for a changing number of rows in your table. This can be solved by just setting the ranges in your equation to a very high number.