Updating pivot source file to specific table source on workname change - vba

I have already scoured the internet (eg. 1 2 3 ) for the answer, but all of them seem to provide a rather convoluted answer, which the users raised
with specific conditions that are characteristic only to their own
project, I intend this question to cut straight to the chase and bit more of a useful general learning resource. The question is:
How can I update my Pivot Table Source to a specific table range?
I have a Worksheet called "Summary" that contains the following ListObject (table) called "t_sum"
Issue is, upon a Workbook name change, the link to the pivot table gets broken and refreshing them returns the following error:
I tried something very simplistic in style of (that is invoked on every time workbook is opened)
Private Sub fix_pivot_source
For Each pivot in Sheets("Summary").PivotTables
pivot.SourceData = "Summary!t_sum"
Next pivot
End Sub
That however doesn't seem to work. Another issue is, given this is a financial report, the data is refreshing and so do the pivot table names. So I can't just directly reference them in the PivotCache
Any idea how can I link to the static table name while taking the Workbook name out of the equation - effectively ommitting it from SourceData check?

Related

How to custom formula for calling data based on a drop down in excel

There is a worksheet with cross tables for each questions.
Now what I am looking for is that if I put a list of questions in a list type drop down and use it to call the entire data only for that particular from another worksheet.
How can I do this?
I tried looking for the question using Vlookup in the entire sheet and it was able to look for that question and bring it back.
Now when I tried using another Vlookup to go inside the table it failed.

Use VBA to get row data from powerpivot data?

I have imported a table in powerpivot, and were wondering if it's possible to get a row data from that source, by using VBA?
Lets say I import a table to my powerpivot datamodel from an external source, that looks like below:
col1 col2
1 tt
2 tg
As the data is not on any sheet, but in the datamodel. Can I treat this datamodel table like an ordinary table with VBA. e.g. get a specific row or sum a column?
I have tried searching alot for this, but google results and excel seems to me abit messy. I have little experience in VBA, and basically have to idea how/if this is possible.
You can reference the DataModel using CUBE functions, which can be called by VBA if you like. For examples, see https://powerpivotpro.com/2010/06/using-excel-cube-functions-with-powerpivot/
But the question is, why would you want to? To some degree, PowerPivot and the DataModel were designed to let you do complicated things right out of the box with little knowledge of code, instead of having to resort to VBA. Instead, just write whatever measures or calculated columns you need, and bring them into the worksheet via a PivotTable.
Well, you didn't give a lot of details around what you are doing, but I think the script below will be a good place to start. modify to suit your needs.
Sub blah()
Set pt = ActiveSheet.PivotTables(1)
Set pits = pt.PivotFields("???").PivotItems
For Each pit In pits
If pit.Visible Then 'only acts on visible items
pit.DataRange.ShowDetail = True
'pit.name 'contains pivot item name that you could use to refer to a workbook to copy the data on the active sheet to, eg.;
'ActiveSheet.ListObjects(1).DataBodyRange.Copy Workbooks(pit.Name).Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1)
End If
Next pit
End Sub

Splitting data between worksheets depending on status of Column A

I have been asked to create a database of volunteers in Excel. The main worksheet (called Data) holds all the information – names, addresses, numbers, reference checks, placements, supervisors, etc. What I am trying to achieve is for the relevant information to be moved from one worksheet to another when the status of the person changes.
There would be 5 categories which the volunteers would fall under (column A labelled ‘Status’)
PROCESSING
ACTIVE
ON HOLD
BARRED
STOPPED/RETIRED
What I want to get is a live database so the information would appear on a relevant worksheet whenever the status on the main spreadsheet changes , but I only want some information to show depending on the category…
Each Worksheet would contain columns A-F from the ‘Data’ worksheet and in addition:
Processing would contain columns X-AE
Active: AF-AW
On Hold: AZ-BC
Barred: AX-AY
Stopped/Retired:- BD-BH
I have searched and searched again but I know nothing about Macros (and my IT department is unable to help) So my question is – is this doable and if so is anyone able to help me?
I hope I am making sense and if not I can email across the dummy database with some made up names to show what it is I am trying to create
You can do this using array formulae. This link shows a simple example which provides the basic formula (explained in detail in the linked article)
=INDEX(range,SMALL(IF(col=value,ROW(range)),ROW(1:1)),COLUMN(A2))
Where range is the range of all your data from your main worksheet and value is the value you want to screen for (this changes for each of your sheets) and col is the column on your main sheet that you want to check the values of. Note it is an array forumla so you have to press control+shift+enter after typing in the formula as explained at the bottom of the link.
You will notice that I have changed the formula to say COLUMN(A2) instead of 2. This is so that you can drag the formula across the columns as well as down the rows. You might need to make this COLUMN(A2)-x where x is an offset because your data don't start in column A.
Note that the same applies for the ROW(1:1) part, if your range doesn't start in row 1 then you will need to offset this by some value as well (i.e. something like ROW(1:1)-y)

Transform and load a large CSV to multiple worksheets in one Excel file

Back Story:
NEW PROJECT FROM MANAGEMENT: I have been given a soft project from my boss to evaluate one of our current ETL plans to look for room for improvement in the process, and I am looking for guidance.
MOTIVE: Excel is currently being used and crashes quite often during the process due to file size.
TASK: Every month an analyst receives a large csv file from a survey vendor containing up to 750 columns (not all unique names) with over 15,000 rows to simply transform a large csv file into an excel file with seven worksheets broken up based on the column headings in the csv. Details of how it is broken up is below.
My question is one large csv being transformed into an edited excel file with multiple worksheets any easier or quicker using VB.NET and VS2010 or VBA for that matter, or would using Excel be the simplilest way to continue this process? I am an Expert Excel user but I am still very much a beginner to intermediate at coding in VBA, VB.NET or any other language.
Detailed Question:
I am open to using free or open source software, but I am most familiar with VB.NET and Excel and Excel-VBA. I have played around a bit coding a simple windows form application to load the csv into a datatable using similar TextFieldParser code found here. I have thought of loading it into an array or even a 2d array to more easily edit the column headings and find the duplicate column headings. The datatable option still leaves me with more questions than answers because I need unique column headings and not sure if I should bother with a datatable if I'm going to just write an excel file right away. I tried CSVreader from CodeProject won't work on files with duplicate header names. I feel as though I am having writers block as I am not sure which direction I should take handle such a process. Any input you can provide will be much appreciated, and I apologize if this question does not have a single and clear best answer, Thanks.
Current Analyst tasks using excel
The current analytical plan has said analyst to open the csv in excel, insert a row above row 1 and use a vlookup to replace the 'New' column names with the 'Old' column names based on a simple two column lookup table on a separate worksheet. For example
New becomes Old
"org-name" becomes "org_name" or
"item_1_Vendor" becomes "item_1" or
"date-created_Survey" becomes "date_created"
etc...checking all sent "New" columns against the list of all possible 750 columns.
Then they paste values of the first row and then delete the 2nd row which contained the New headings we want to change.
Then the analyst has to fix the primary key on the file which is called "sid".
The Survey ID field (sid) should have a number for each row of the data file. Sometimes the sid shows up under the sid_HCAHPS or the sid_CGCAHPS fields instead.
The analyst would insert a column next to the "sid" field and put a formula in it like this, for example:
=IF(BE2<>"",BE2,IF(RD2<>"",RD2,IF(UH2<>"",UH2,"")))
Actual cell references would change but in the example excel formula,
"sid"=Range("BE2")
"sid_HCAHPS"=Range("RD2")
"sid_CGCAHPS"=Range("UH2")
Once the newly created primary key column is made and filled without blanks, we can delete the original "sid" column.
The next step is to check the columns because there may be a redundant HCAHPS section of columns (due to a second survey being sent and then returned- coded as Wave 2), delete second set of columns "sid_HCAHPS" through "language"
Next is the largest alteration because we have setup a system where we send this information to our database admins in the form of a seven worksheet excel file to be loaded by an MS Access Query that creates a table from each sheet that gets loaded into our proprietary business intelligence software. All Done!!
Is your question, "can VB.net automate our current analyst tasks?" -If so, then yes.
You could use the streamreader class to get data from your csv
(http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx)
Then store it either in an array as you mentioned or use the *list class
(http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx)
Once you've got all your data stored you'll need to automate excel, this is quite straight forward but here's a link to get you started with that as well: http://support.microsoft.com/kb/301982/en-gb
With the list class you can create a list of custom objects using either classes or structures. eg.
We define a structure:
Structure rowOfData
Public intPrimaryKey as Integer
Public strIceCreamName as String
Public decPrice as Decimal
End Structure
We can then create a rowOfData and add properties to it:
Dim iceCream1 as rowOfData
iceCream1.intPrimaryKey = 1
iceCream1.strIceCreamName = "Mr Whippy"
iceCream1.decPrice = 0.99
We create a list with:
Dim listOfIceCreams as New List(of rowOfData)
And add to it like this:
listOfIceCreams.Add(iceCream1)
listOfIceCreams.Add(iceCream2)
etc.
And access the members of the list like this:
listOfIceCreams(0).decPrice 'gives us the price of the ice Cream that was added to the list first.
There are also a lot of other useful methods that lists have which arrays don't. You could have a look through that msdn list class link to see if anything jumps out at you that you might need

Comparing two columns and matching values from seperate work sheets

The goal of my project is to create an out of office program that will allow easy tracking and auditing of our Sharepoint site as it doesnt have a built in system to do. I have no background in VBA, but I have done quite a bit of python. That being said I've ran into my first issue. I'm not sure how the syntax works, and what commands I should be using to get the results I want. I.e. sheets vs worksheet vs worksheets.
I have a workbook, 1 sheet is Raw Data, in which I import data from a sharpoint site. It displays the following columns
Resource Name -- Absence Type -- ID -- Start Time -- End Time -- Created -- Modified by
The next sheet I have is tracking, it's called Tracking. On this page the user imputs Resource Names they want to track into Column A, and then the remainign columns are going to display the number of absences that name has so it will look something like
Resource Name -- Vacation -- Sick -- WFH
Clooney, George -- 2 -- 0 -- 7
A counter will run based on each instance that appears in raw data and adds the number to the counter based on the absence type from raw data.
I need a way to loop through Raw Data and look for the names that appear in the Tracking data. If Possible I'd like to store them in a third worksheet jsut for testing purposes. I know the logic I need to use, but what I dont know is the syntax to refrence the pages together. Any insight on the best way to accomplish this?
Question : I need to Search raw data for every instance Resource Name appears in it from the Tracking page and store into another worksheet.
If you don't want to use PivotTables (can be hard to search later) this is the way to do it with COUNTIFS. This formula goes in the "Sick" column of Tracking in row 2 (assuming row 1 is headers).
=COUNTIFS('Raw Data'!A:A,Tracking!A2,'Raw Data'!B:B,"Sick")
It assumes that in Raw Data Name is in column A and AbsenceType is in Column B, but it doesn't matter how many records there are.
The way I understand your question (and it wasn't easy), you are dealing with a bunch of timesheet info. You seem to be trying to count the number of instances of different kinds of time off that people are taking - whether that be vacation, sick or working from home(WFH).
I've never heard someone's name referred to as a "Resource Name" lol.
You really don't need to use VBA for this problem - at least not anything that you can't just record a macro for - it seems to be a rather simple problem that can you solve using a pivot table.
If you want to you can set up a vlookup reference to this pivot table to create the little form that you seem to be trying to create. But really I think your better off just teaching whoever is going to be using this about pivot tables. Let me know if I misunderstood your question and I'll be happy to delete this post.