Excel: Selecting Specific Columns and Rows in Pivot Table and Formatting - vba

I'm using a Pivot Table in Excel 2010, and while searching posts I find that a lot of users are frustrated like me because it doesn't keep all formats.
So what I'm trying to do is run a macro that formats columns in a Pivot table, but limited to the last row and column in the table. I have the formatting info, but I just need to know how to apply it to specific columns and rows.
What I was thinking might work is finding the last column of the Values row, in this case "Stops per Rte" which is the last Values column; however, I have months listed at the top, so it repeats across months. If the user filters only certain months then the # of columns will decrease.
Same goes for the # of rows: of course, the user should be able to expand/collapse rows as needed, so I only want the column format to go to the last row or just above "Grand Total", if possible.
Hopefully, this makes sense. Thanks in advance! = )

Related

Update Excel Pivot Table Fields

I compile morning reports every day using a workbook with a number of different sheets and pivot tables. So far, I have been able to somewhat automate the data retrieval process, and getting the pivot table data to refresh was very simple. What I would like to do now is get the pivot table field filters to change automatically to include the data entries for thirty days prior up to and including today's date. I have seen some things out there that let you select just one date, but I need to select a range. I have tried using a relatively recorded macro, but it gives me this
Sub Update()
Sheets("Doorline Month").Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.PivotTables(-1).PivotFields("Date").CurrentPage = "(All)"
With ActiveChart.PivotLayout.PivotTable.PivotFields("Date")
.PivotItems("1/14/2018").Visible = False
.PivotItems("2/15/2018").Visible = True
End With
End Sub
I believe that this would only deselect 1/14/2018 and make 2/15/18 visible. I want this code to work automatically every morning, so if I come in on 2/16/2018 it deselects 1/15/2018 and selects 2/16/2018. I have tried replacing the dates in the recorded macro with "ActiveCell" but to no avail. Any help is greatly appreciated. If it helps, the data for my pivot tables is in sheet 1 and the dates specifically are in column A. Unfortunately, the number of data entries per day is not consistent so I don't know if that complicates things or not.
Why dont you put a flag in the database/ data of origin that marks with 1 or 0 each line depending if the date is within the range you want or not? Then you can leave the 1 or the 0 selected in the pivot table's filter field and the relevant data would show automatically every time you refresh.

Merging two tables while deleting duplicates in the first table and

This is my first post here and your help would be greatly appreciated! I've read a lot of other posts on this site, however I cannot find the answer to my specific question. I tried using VLOOKUP, INDEX, MATCH, Pivottables, etc. However it doesn't work out the way I want.
Background information: for my thesis I'm studying the difference in cost of capital among single segment and multi segment firms. My dataset contains two tables, one with the SIC codes of the firm's segments and one with the corresponding sales of that segment. The issue with these tables is that there are duplicate SIC codes in the first table. I want to remove the duplicates from the first table and simultaneously calculate the sum of the sales of these duplicate SIC codes in the second table.
My data looks as follows:
The SIC codes per segment and the sales per segment:
Input of SIC codes and sales per segment (one company for 20 years)
What I want to do is eliminate the duplicate SIC codes. If I change the table with SIC codes I also need the table with sales to change accordingly. However, the sales of duplicate segments should not be deleted but added to the first duplicate segment. I can computed this manually for one company, however for 1800 companies would this would be very time consuming. The manually computed output for the SIC codes and for Sales looks like this (so I don't need to merge the table, the output is still in two different tables):
Required output for the SIC codes table and Sales table (one company for 20 years
Thanks a lot!
This is the way to go about it. In 2 parts: get a list of unique SIC codes in each row, and, sum up the unique corresponding values to them.
Part I:
General logic to this part: nested INDEX functions.
Let's assume your output table (range including headers A25:J45) is exactly below your main/input table (range including headers A1:J21). Assuming only 20 rows of data, but you can drag the formulae to as many rows.
The first column should always be picking up values from the corresponding first column of input table. A26 =A2 and so on.
For B26, use this formula =INDEX($A2:$J2,MATCH(0,INDEX(COUNTIF($A26:A26,$A2:$J2),0,0),0))
You can drag this formula across to J45/end of output table. (You can use "Evaluate Formula" in excel to understand the workings of the logic)
This should populate your output table with unique SIC codes for each row.
Part II:
General logic to this part: SUM and Arrays
Let's assume your output table (range including headers L25:U45) is exactly below your main/input table (range including headers L1:U21). Assuming only 20 rows of data, but you can drag the formulae to as many rows.
In cell L26, which is the first row, left most element in the output table, you will need an array formula (Ctrl+Shift+Enter). If you don't know what array formula are read here.
Formula for L26 {=SUM(IFERROR($A2:$J2=A26,0)*IFERROR($L2:$U2,0))}.
You will need to enter only this in L26 =SUM(IFERROR($A2:$J2=A26,0)*IFERROR($L2:$U2,0)), and hit Ctrl+Shift+Enter. Excel will put the curly brackets around it on its own self.
Copy paste the formula in the rest of the output table.
Screenshot for reference based on your Excel file.
There is a remove duplicates function in excel 2010 on the Data Tab Screenshot

excel vba: How to Sort Group of Row based on column value

I have the following data in my excel. and my requirements is to sort a group of rows based on Forecast Age in Descending order. I have already tried the built in sort function in excel but it doesn't meet my requirements.
Let's say for example my forecast age is in Range("BO54")=17 and Range("BO88")=19, Range("BO88") value is bigger than the other so it should to be on the top. Range("BO88")'s group is Range("A78:CA91") .
In short I want 88th row and it's group of row which is Range("A78:CA91") to be at the top. Is there a way to do this without sorting the data within Range("A78:CA91")?
Well I can suggest you one way which might be lengthy to code.
First you can run a loop on all rows with a condition that if "Description" = "Forecast Age" then paste it in a new sheets. This will give you list of rows with every "house" and age. Sort it based on age and add a column which will assign rank() it based on age.
After this add a rank column in your main data and vlookup the above obtained ranks to each house and then finally, sort the entire table based on these ranks.
You should obtain the result you expect.
This is just a heads up. You can optimize the process and improvise it.

Powerpivot one to many filter column

I have 2 tables, a visit table and then a diagnosis table. Visit is my one and diagnosis is my many (you can have secondary and tertiary diagnosis).
I have over 50 measures created in the visit table. Because of this, I simply want to create a flag on my visit table in the form of a column that will filter down my results based upon selections from the Diagnosis table. I have seen formulas for measures that use =calculate([cases],DXCodes) which will filter down the cases to only those that relate to the DXCodes and this works, but I don't want to have to build this in for every measure. Instead I want a DXFlag that will flag my visit rows with a 1 if what I selected from Diagnosis exists in the Visit table. Suggestions? I can get more specific if this is not making sense
Making some assumptions about your data, you could have tables like this:
And if you create a measure to pick out each of the diagnoses:
[diag1]:=FIRSTNONBLANK(Visits[Primary Diag],1)
[diag2]:=FIRSTNONBLANK(Visits[Secondary Diag],1)
[diag3]:=FIRSTNONBLANK(Visits[Tertiary Diag],1)
Then add this measure to flag your visit rows:
[Found]:=MAXX(ALLSELECTED(Diagnoses[Diagnosis]),IF(FIRSTNONBLANK(Diagnoses[Diagnosis],1)=[Diag1]||FIRSTNONBLANK(Diagnoses[Diagnosis],1)=[Diag2]||FIRSTNONBLANK(Diagnoses[Diagnosis],1)=[Diag3],1,BLANK()))
This gives you this result:
Edit: Response to revised/clarified requirements
I've been trying to see if I can get a calculated column to take account of the diagnosis selection with little success. Doing it with a measure is fairly straightforward:
[Diag Flag]:=IF(CALCULATE(COUNTROWS(DiagnosisTable),FIRSTNONBLANK(DiagnosisTable[Diagnosis],1))>0,1)
But this approach would require you to revise all the measures you wish to appear on your output summary. If it's just the total number of visits and the sum of the length of stay, that's not difficult, but if you need to revise all 50 measures it's a bit less appealing.
Here are the new measures for the number of visits and the length of stay which I have shown in a separate PivotTable on the same sheet:
[Cases]:=SUMX('Visits',[Diag Flag])
[LengthSum]:=SUMX('Visits',Visits[Length of Stay]*[Diag Flag])
And the output:
NOTE: My slicer is connected to both tables.

Excel randomly select name from list with multiple entries

I have an excel 2007 worksheet with employee names in column A and total number of entries in column B. I need to be able to randomly select x number of employee names from the total number of entries, allowing for the fact that some will have multiple entries.
For example:
Amy............30
Brian..........12
Charlene.......15
Michael.........1
Nathan..........7
What is the best way to do this?
My initial thoughts are:
1) find the max() of column B occurances of a random number in another column, like C. Then find the top values for all of that new column.
2) create a VBA array of all of the potiential entries and randomly pick one from there.
3) loop through all of the names in column A and create a temp worksheet with column B instances of each, then assign a random num generator and choose the top n.
Having said that, there may be something a lot easier. I am not sure where to begin. Normally I can find code that is similar to what I need, but I am not having any luck. Any help that you can offer would be appreciated.
Thank you in advance.
I would probably do something like this if I understand your question correctly(I just read your question title):