Retaining field formatting on pivot tables - excel

I have a dynamic pivot table that updates with a macro every time a user inputs new values in a "what if" template. The rows I would like to be in dollar format but everytime a new value gets entered they reset to general.
I have already clicked "preserve formatting on refresh" and it doesn't work.
Additionally, sometimes when the what-if numbers are updated the rows expand and sometimes they don't. How can I always keep them collapsed?

Related

Value Field add event in pivot table

Background
When the Pivot table seems to have a custom NumberFormat, the created fields will not carry it.
OffTopic: I guess it is normal, sometimes it displays 2, 3 or 5 digits among the same dataset, this is for convience for the user (thus making each cell in the column likely to have a number format different from each), I am not really able to change it, there is another ruling that basically formats cell by cell on the source data (I know it's not ideal, but it is what is convinent for the user and avoids overflooding the workbook with conditional formattings).
I would like to keep that number formatting on the value field when the user adds it to the PT by dragging that field to the Value field.
Problem
I have not found the right event to detect when the field is being added to the Pivot Table, I have tried all the events related to PivotTables in the worksheet, but even by reading to their inputs, I can see that such event may not exist.
Question
Is there an event that detects the field adding?
Further thoughts
I am not quite sure if it exists, my alternative would be to create a button where the user would get the format from the original source by clicking it by using the same code that I use to format cell by cell, but I would like to this be ran automatically when the Value add happens.

Two tables with no unique values that I need to match and return value based on date range and zipcode fields

I have two sets of data; the first (Wind Claims) contains a StartDate, EndDate, and Zip Code field. The second (PLRB Wind) contains a Date, Zip Code, and Wind Speed field.
My goal is to get the Wind Speed from the PLRB Wind tab to the Wind Claims tab if the Date from the PLRB Wind tab is between the StartDate and EndDate on the wind Claims tab AND the Zip Code from the PLRB Wind tab matches the Zip Code on the Wind Claims tab. The point is to identify the wind speed where damage was reported.
I have tried a couple formulas; this one I actually got results but only 1227 out of 16822. I wouldnt expect to have a 100% match but definitely much more than what I am getting. I think the reason is because this formula is looking for the specific date and not looking at the date range:
=XLOOKUP(Z2&N2,'PLRB Wind'!$I$2:$I$78525&'PLRB Wind'!$D$2:$D$78525,'PLRB Wind'!$M$2:$M$78525,"")
I also tried an Index Match (this is just the Match piece of the formula)
=MATCH(1,IF('PLRB Wind'!D2>=$B$2:$B$16823,IF('PLRB Wind'!D2<='Wind Claims'!$C$2:$C$16823,IF('PLRB Wind'!I2='Wind Claims'!$Z$2:$Z$16823,1))),0)
Thank you in advance for looking at this. I appreciate any help you might be able to provide!
I'd use power query for this. Do you know what power queries are?  I was upset when I found out because of all the useful ways I could have been using it before.
You might feel differently, though. Create a new copy of your workbook for this just in case you hate it. :-)
In the "Data" ribbon of Excel, in the Get & Transform section, there's a "From Table" button. Highlight your PLRB table (including the column titles) and click that "From Table" button to create a new query from it. It will create the table and the query.
A power query editor window will pop up, presenting your query as two steps, listed in the middle of the right sidebar. The first step is to get the information from your worksheet. The second step changes the data types. Click the icon to the left of each date column's title to change the type from datetime to date because why not. On the right sidebar, change the query name to PLRB.
Now click "Close & Load" on the home ribbon. It will create a new tab with the results of your table. Leave it for now. You can delete that tab later and it won't delete the query.
So, back to your worksheet, highlight the column-title row and data rows for first three columns of the wind claims table. Create another query from table. Call it WindClaimsInput. Again, correct the datetime columns to date columns
OKAY, so now you have two queries. They both read from your workbook but they could have been from another file or text file, etc. If you like this solution then your final form might be a worksheet that doesn't actually have any source data in it, just queries that gets the raw data from elsewhere and a tab that presents the third query we're about to make.
Now for the fun part.
While still in the power query editor editing your WindClaimsInput query, near the left edge of the "Home" ribbon there's a button named "manage...". Click it, then click "Reference" to create a third query that starts with the old one. Remember, queries are only instructions. We aren't copying data until we run the queries.
Now, find the button to add a column. It should open a dialog box asking the column name and formula. Name it "PLRB" and use this formula: Table.SelectRows(PLRB, (r) => (r[Date] >= [CATFromDt] and r[Date] <= [CATThruDt] and r[ZipCode] = [ClaimZip])) Table.SelectRows is a power query function that takes two arguments:
The table (or query that returns a table), and,
A function to run on each record (aka row) of the table and return true/false. In this case, we created a function that takes one argument (r) and returns true or false.
So the above formula says "Give me a table of all rows in PLRB for the given ClaimZip zip code that also has a Date between CATFromDt and CATThruDt." Since it's a column formula, it runs once per row in. Wind Claims.
Now you have a table where the last column is another table! Specifically, the rows from PLRB that are relevant for that Wind claims row. You can single-click on any of those cells in that last column to see the subtable.
To right of the last column's title will be a little "expand" icon. Click it, choose to aggregate by max wind speed. (The right edge of the "wind speed" choice will let you change it to maximum, or average, or whatever you like.) Unclick "Use original column name as prefix". Click okay. Don't worry, you can delete this new step and try again if I didn't describe it well.
Hit "Close and Load" to see it in your workbook. If it looks right, great! Otherwise, feel free to go back and edit some more.
And now you're done! Unlike formulas it doesn't automatically refresh but when you want to refresh your output based on your input tables you can refresh that query or, in the "Data" ribbon you can click "refresh all".
In the data ribbon of Excel, in the "Get & Transform" section, there's a "Show Queries" button that toggles a sidebar that displays your queries you've made. You probably only want to keep loading your third query, so you can change the "Load to..." of the other two queries to "Connection Only".
Sorry I can't do screenshots right now.

What is the default selection in an Excel pivot table

I have come across numerous definitions of how Excel adds the field into a pivot table when selected, for example:
You can quickly add fields to your pivot table by using the check box next to the field name from the field list in the PivotTable Fields window. This can save time if you have a lot of fields to add instead of dragging and dropping each item. Fields containing text data will be added to the Rows section and fields containing numeric data will be added to the Values section when using the check box.
Source: howtoexcel.
However, here's a ten-second video showing that this is wrong -- at least in my case using Excel on Mac:
https://gyazo.com/f299b74d6f6fa4d74737e3f88362538c
Adding a float does a sum (makes sense), adding a text field does a count (??), adding an integer does a row aggregation (??), and adding a date does a count (??). Other than summing up a float, which would be common for financial purposes, I'm not sure what the rhyme or reason is for the defaults on everything else. Is there definitive documentation on what the default selection is by excel or more info on how this is done?

How to predefine the required format of a cell?

I currently build a dashboard in Excel to track forex trades. One worksheet is for adding new trades to the database. Within this worksheet i have several fields that the user needs to fill in order to add the trade to the database. Now I want to avoid that the user inserts wrong data (regarding format) in the cells and therefor crashes the outputs from the database later on.
How can I define such criteria via VBA? For example the field date to have the format YYYY-MM-DD and the field Trading Volume XX,YY so the user can't (accidentally) insert X,YY etc. For dropdown fields it is easy by defining the inputs within the dropdown menue itself, but i need some solution for the manually filled cells.
I will then define a msgbox to pop up and inform the user to meet the required input formats.
Thank you very much in advance!
Some key words would help me, I can then get into deeper it and code it by myself.
Rather than using code could you not use data validation and force entry of a date, numeric value etc.?
If you define the cell format to be custom 'yyyy-mm-dd' then apply data validation with a defined date range Excel will auto format it for you even if they enter dd/mm/yyyy and alert if an invalid entry is added.
Then for a belt and braces approach run a routine at Save that will mimic what your database will do and alert if there is still invalid data see https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.beforesave
I would also lock down the sheet to avoid users adding rows / columns or moving your cells around.
enter image description here

How Do I create a Data Validation List based on a filtered TABLE?

I have spent hours searching for an answer and attempted to create my own solution with little pieces of information I gleaned, but I am still no closer to a solution.
Scenario: I have a data table (sorted by index field that I want to use). I have a CRITERIA column and the INDEX column. I have a form worksheet where I want the list to appear. On a second form worksheet I have VBA code attached to a button(s) that filters the data table in-place. On the form sheet there is a SELECTION CRITERIA field that, based on the value selected from the pull-down 'data validation' menu, dynamically populates the form fields from the values on the corresponding row in the data table. (this is accomplished by a VBA event trigger to the cell, and population is accomplished with other VBA code. The event trigger also prompts user to write changes back to the data table if changes are identified from the previous populated form.)
PROBLEM: I want the data validation list on the SELECTION CRITERIA cell to dynamically change based upon the auto-filter triggered by the button selection. (each button runs a VBA procedure that filters the list based upon one or more criteria)
I am most grateful for any suggestions or solutions provided!

Resources