Summarize data in Excel with VBA - 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!

Related

Find duplicates Value and sum a duplicates using index number Using VBA

I need a little help here for excel and VBA, lets says my column like this
No Name Rank Point
1 Foo 1
2 Bar 1
3 Jhon 1
4 Doe 1
i need to fill a point using adds the same rank by using index of number and then divide by the number of duplicates value, point = (1+2+3+4)/4 i need an help for using a VBA script, any of help will be appreciate
You dont need VBA for thet. If your data is from A1 to C5 (with Point in Column D), lke in your example, put the following formula in D2 and pull it down:
=SUMIF($C$2:$C$5,C2,$A$2:$A$5)/COUNTIF($C$2:$C$5,C2)
If you need VBA, just add Application.Worksheetfunction to both formula parts.

(VBA) Extract and copy and paste column values for specified date range for corresponding row values

I've been stuck on this for a while now. What I need is the title - what I have is:
Sheet 1:
1 billion mostly unnecessary columns.
Column D: Values I need Column F: Column M:
Revenue Names Date
12 John 1/24/2016 2:40:02 AM
15 Sarah 3/2/2016 4:35:17 PM
14 Sarah 7/17/2016 1:50:10 PM
20 Matt 8/20/2012 4:16:12 AM
10 John 11/19/2015 5:04:05 AM
etc. etc.
Current Sheet 2: Pivot Table*
Row Label:
Sarah
Matt
John
etc.
Desired Sheet 2 Pivot Table+*
Row Label: Column __:
Revenue
Sarah 29
Matt 0 *(note: see below, but = 0 because Matts value corresponded to date 2012)
John 22
etc.
The important thing about Sheet 2 is that I would like to tell VBA to find the nearest emptiest column within the sheet, and input the values from Column D from S1 (and sum for duplicates) but only sum and extract values that range from today's date to 11 months prior. Currently I made a module to automatically create a pivot table for the next sheet, but I have wanted to add the above for a while, just stuck.
I'm also assuming I won't have to specify sum if I tell VBA to extract data for corresponding row label names? And I was thinking about putting a Begin Date and End Date cells on Sheet 2 to refer to, or using the TODAY() function somehow for the date, but not sure how to specify to extract and sum values for the date range from today to 11 months prior.
Edit: *Please note this is a watered down version of my current situation, everything is bigger so to avoid confusion, pivots are necessary, but I choose to dilute it for the sake of this issue.
Do you really need a pivot table?
If you dont, just create a new sheet with all the names, set a cell for the starting date you want to consider and do =sumifs
For example
On Sheet2 B1 you put 1/24/2016.
Then on A2 you insert the name (Sarah, for example)
On B2 you put the formula
=Sumifs(Sheet1!D:D,Sheet1!F:F,A2,Sheet1!M:M,">"&B$1)
After that just make a list of name and drag the formula beside every name.
*My excel is in portuguese so maybe the formula needs some other ajustments to work.

How to fill cells based on complicated formula?

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.
     

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

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.