Excel VBA macro create new value from 2 entire column - excel

I need to sort values from an entire column.
I do have one column city which have different values of 0 or 1 ( 0% and 100% )
There are 90 000 rows.
I'll need to do a percentage by compiling the values of 0 and 1 by doing the average
I want that value to appear in the column I
Like what I want is that:

You can use AVERAGEIF combined with IF
Function
AVERAGEIF
My fake data:
My formula at C2 (and drag down)
=IF(A2=A1;"";AVERAGEIF($A$2:$A$13;A2;$B$2:$B$13))
Anyways, I would suggest to use indeed Pivot Tables:
Create a PivotTable to analyze worksheet
data
With Pivot Tables, you could group your data using cities field, and then calculate the average easily, and just 1 single row per city instead of those empty rows

Related

Conditional Formatting in Pivot Table in Excel based on text field

I would like to color code a pivot table based on a text field within the data.
This is a mock up, as I'm at a loss as to how this can be done. One of the tricky parts is that you can't actually display the text field that is the basis for the color.
Thank you,
Neil
Contract
Month
PayStatus
Payment
1
Jan-21
Complete
90
2
Jan-21
Complete
95
3
Jan-21
Complete
93
4
Jan-21
Complete
94
1
Feb-21
Pending
91
2
Feb-21
Complete
95
3
Feb-21
Complete
92
4
Feb-21
Complete
94
2
Mar-21
Complete
96
3
Mar-21
Pending
93
4
Mar-21
Pending
94
2
Apr-21
Complete
95
3
Apr-21
Pending
94
Mock-up Pivot Mock-up (color coded)
You still don't need to use VBA to get the results you're looking for, but there is a little work to do in order to set it up. The solution is to use Conditional Formatting in which the formula is VLOOKUP. To make it work, you need to modify your table slightly.
VLOOKUP works by finding a unique value in the first column of a table or range. The first column values in your table are not unique, nor are the values in the second column. But if you combine the first and second columns, you can create a unique value. A column must be added to the left of your data to create this unique value:
The formula in Column A combines the values in columns B and C:
=B2&"+"&TEXT(C2,"mmm")
(There's a reason to format the date as just the Month, more later)
What we're ultimately looking for in your formatting rules is when any value in the pivot table, for a given month and contract, matches either "Complete" or "Pending". Because we've set up the data with the helper column, we can now create a `VLOOKUP' that can work for us. So we need a formula that does this:
VLOOKUP(unique value based on month and contract,get the pay status) = "Complete"
VLOOKUP(unique value based on month and contract,get the pay status) = "Pending"
The formula in the VLOOKUP has to reference back to the source data (when it's inside the pivot table). In order to lookup that unique value, we have to create it by combining values from the pivot table. If the VLOOKUP formula was in Cell B6 in the pivot table:
Then the formula will be:
=VLOOKUP($A6&"+"&B$4,Sheet1!$A$1:$E$14,4,FALSE)="Complete"
Notice how the "lookup value" is not a single cell, but we're creating the same unique string/value in the helper column.
In your pivot table, click in the values area ("Sum of Payment") in Cell B6, then select Home -->Conditional Formatting-->Manage Rules. Next "Add New Rule" and then make sure your rule looks like this:
Create a second rule in the same manner and apply it to the pivot table.

Fill in table based a column of categories in Excel

I have a table that looks like this:
Type Value
Movie 5
Food 3
Gas 10
Food 2
.... ....
And There's a second table I want to fill in with "Value" based on their type in the first table, so that the corresponding rows look like this:
Rent Food Movie Gas Clothing ... ( appear in specific order bc they are subcategories)
5
3
10
2
The title row is already there, so I was thinking there might be some kind of lookup method to do this? How do I do that?
your second table apperas to hold one value per row but it doesn't have a label. it does correlate to the original row number, is this by design or coincidence?
if this is by design then you can use those 2 columns, hide them if you like, get a unique list of categories by copying you r abels to a new colum, removing duplicates in the data tab, then paste special transpose in c1 to create colum headers.
so column a and b remain unchanged
row 1 contains header starting at column c
your data starts at c2
this is the formula
=Iferror(vlookup(C$1,$A2:$B2,2,false),"")
drag it down and to the right
you can copy paste special values when done to remove the formulas
for something with only a hundred or thousand cells this will be one of the easier options but i would not do this on large tables, for those i would use power query or VBA
Assuming your 1st table is in Sheet1 and 2nd table is in sheet2.. you may try to fill in Sheet2!A2
=IF(Sheet1!$A2=A$1,Sheet1!$B2,"")
and drag it all the way.. Hope you get how it works.. and what you need.

% of Row Total in pivot table

I'm trying to format my pivot table values as percentages of their row totals (in my data, each quarter). However, when I select Show Values as % of Row Total, it gives each value 100%.
When I try Show Values as % of Parent Row Total, it uses the column as the total and uses the first 4 quarters of 2017 as % total of 2018, not at all what I want to do.
Here is my current pivot table
Here is how I'm trying to format the same pivot table
I didn't type all your "types" (or whatever is in the columns), but when I summarize by % of Row Total, it works as expected.
Is yours set up differently than what I show?
Update
If your data points are all separate columns as #jeffreyweir suggests, then you can't summarize them how you want using pivot table features. You'll have to add some columns to your data
Each Pct column is a formula:
=[#Replacement]/SUM(tbldata[#[Replacement]:[DTK]])
=[#Refund]/SUM(tbldata[#[Replacement]:[DTK]])
=[#Parts]/SUM(tbldata[#[Replacement]:[DTK]])
=[#DTK]/SUM(tbldata[#[Replacement]:[DTK]])
Then you can sum those in a pivot table.

Sumproduct using 2 criterias and on filtered values

I have the following source table
Date | fruit | veg
The user has previously filtered the date by month. In another table, I want to count the number of apples and tomatoes for example. To remove the invisible rows I use the SUBTOTAL function as
=(SUBTOTAL(102;OFFSET(limiter;ROW(limiter)-MIN(ROW(limiter));;1;1)))
where 'limiter' is the date range.
Then to count the number of apples in the fruit column , I use
= SUMPRODUCT(SUBTOTAL(102;OFFSET(limiter;ROW(limiter)-MIN(ROW(limiter));;1;1)) * Table!fruit = "apple"
and it works fine.
But, if I want to add in the vegetables criteria along with the fruit, the result is 0. I tried doing the sumproduct of fruit and veg first and then add the subtotal function but it gave a huge number.
Have you considered using a pivot table? (I strongly recommend you to)
Go to a new tab, select A1 and insert, new pivot table. You can put the fruit field in the column (probably row labels - my excel is in Portuguese =/). You would instantly have a table containing as first colum the list of existing fruits.
Then add the date to the body (probably values) of the pivot table and certify the type of value shown is count.
You would intantly get the count of all fruits.
The great advantage is that you can play around with this table as easily as dragging and dropping fields in columns, rows and body/values and instantly getting the results you want without having to figure out any formula issues.
Pivot tables also allow you to filter not only the date, but any other fields you wish.

Excel: Match\Index specific column based on multiple criteria

I have two Tables, Table 1 Column A is a rolling date column. Table 2 consists of four columns of differing "Trigger Dates". Table 2, Column 1 contains an Annual trigger, ie occurs once a year. Table 2 Column 2 contains Bi-Annual Triggers, and occurs twice yearly. Table 2 Column 3 contains Monthly Triggers, "12 dates" and Table 2 Column 4 contains Weekly Triggers, "52 dates".
Ok, so what I'm looking for is to a formula that will return the date trigger, where Table 1 Column 1 matches a condition (Annually, Bi-Annually, Monthly, Weekly) selected in a cell "we'll say F1, outside of any ranges that may be created.
Example: If Table 1 Col 2 = "01/02/2013" and I select "Weekly" in F1, the formula will look in the weekly Column in Table 2 to determine if "01/02/2013" exists.
I've allocated a named range to each column in Table 2
I've allocated a named range to the condition in F1 which is a drop down list
Any suggestions?
You might try:
=IFERROR(VLOOKUP(B2,INDIRECT(CHAR(72+MATCH(F$1,$I$1:$L$1,0))&":"&CHAR(72+MATCH(F$1,$I$1:$L$1,0))),1,0),"")
Ok, so supposing your Range Name is in F1, your formula to find the match would look as follows:
=If(IsError(Match(LookupVal,Indirect(F1),0)),"No Match", "Match")
...Something along those lines
Hope this helps point you in the right direction.
INDIRECT and VLOOKUP will be your friends here.
I constructed Table2 just as you had described, with the Annual,Bi-annual,Monthly,Weekly labels for the columns in the table - this will avoid having to keep named ranges up to date, as it's looking directly at the table in the formula
For the function, I then used VLOOKUP to find the date, in the column referenced by F1.
Column Reference: INDIRECT("Table2["&$F$1&"]")
Find Value: VLOOKUP([#Col2],INDIRECT("Table2["&$F$1&"]"),1,FALSE) (looking at column 1 in the return values, as I don't really care what value is returned.
This will return the date if found, and an error if not. I can then wrap the result in the IF(ISERROR( so I can return Yes or No if the date is found.
This produces the entire formula of
=IF(ISERROR(VLOOKUP([#Col2],INDIRECT("Table2["&$F$1&"]"),1,FALSE)),"No","Yes")
for the next column in Table1, so that when I change the value in F1, it will look for that column name in Table2, and let me know if it is found.

Resources