Use VBA to get row data from powerpivot data? - vba

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

Related

Query several ranges and add automatically a column to know the source of each row

I am trying to achieve the following in Google Spreadsheets.
First, I want to query several ranges (in different sheets from the same spreadsheet). I tried a formula like this =query(arrayformula({indirect(E2:E10)}),"select * where Col1 <>''") with no success
In E2:E10 I have a list of ranges. Column F contains a name that describes the source of the value in Column E.
My second problem is that I need to add a column to the output of that query that tells me the origin of each row.
If the sources are ranges of 3 columns by country I need to merge those tables and add that country to each row.
All credits to +Ben Liebrand who helped me out here: https://support.google.com/docs/profile/3464
"I just want to start of by saying that the indirect() function does not work in an arrayformula() function as expected. So you will need to take another approach. I can understand what you are trying to do so I added another TAB in your spreadsheet to demonstrate another approach. I know it was initially a specific design you were trying so I made some changes to what you had. Maybe you can take a look at what I have offered and maybe you can tweak your design.
I know what I am offering is just very rough but you will also notice that I removed the end row specifier from your ranges in the range table.
Don't assume my example to be the final result but I was just trying to show that the range you were trying to use with the indirect() function will not work.
So hopefully this will give you a new idea of how you can maybe handle this.
My formula also adds the country to each of the tables in the output. My formula looks like this
=query(ArrayFormula({
if(len(indirect(regexextract(F2,"\w+\!\w+")&":A")),G2,),indirect(F2);
if(len(indirect(regexextract(F3,"\w+\!\w+")&":A")),G3,),indirect(F3);
if(len(indirect(regexextract(F4,"\w+\!\w+")&":A")),G4,),indirect(F4);
if(len(indirect(regexextract(F5,"\w+\!\w+")&":A")),G5,),indirect(F5);
if(len(indirect(regexextract(F6,"\w+\!\w+")&":A")),G6,),indirect(F6);
if(len(indirect(regexextract(F7,"\w+\!\w+")&":A")),G7,),indirect(F7)
})," select * where Col1 <> '' ")
Hope this is of some help to you"
And I hope is useful to the community
GerĂ³nimo

Display change in value of a cell in adjacent cell using excel VBA

I have a excel sheet that display's price on certain items in a column by looking up amazon API using excel vba. The price of may change overtime. So I am trying to display the difference in prices each time i run my macro, in a cell adjacent to the cell that displays price.
But I am not sure how to achieve this. Can any body guide me on how to achieve this?
This is just a sample, it must be adapted to your schema and data layout. Say the prices are stored in column A from A1 to A100. Say you already have a macro called RefreshData() that updates column A. In B1 enter:
=C1-A1
and copy down. This macro store the current values in column C before refreshing the data:
Sub DoUpdate()
Range("A1:A100").Copy Range("C1")
Call RefreshData
End Sub
Column B will display the price difference.
Something like this?
Let's say your data are in a range A2:A10
Dim rng as Range
Set rng = Range("A2:A10")
rng.Offset(0,1).Value = rng.Value
Run this before you run your original macro to store the values in an adjacent column before the values change. You may need to make the range dynamic, depending on your needs.
Without seeing you code, I cannot give a detailed answer. However, I ran across a similar problem once, not using Amazon API though, but a sharepoint connection.
If the amazon api is somewhat similar to the sharepoint stuff, I guess it refreshes cells when you click "update", or run the update sub. In that case you will have to either create an array to store the old prices in vba (very slow process), and then write them to your table, or create a separate tab where you store the item-lastPrice combination.
I ended up storing not only current price but all prices and the date/time of the price, to be able to see change over time.
For the copying of data itself using VBA, either of the above methods should work. In my initial code I used vba loops :-p, but copying using excel functionality is much faster.

Access - Create Select Statement via Excel Template

to export data from an Access 2010 Database I would like to use an Excel template as seen below.
In the first row I can define columns that are in the database. In the second line I can define exactly 1 filter which will be used in the "where" statement.
When pressing a button in Access, a query that is based on the defined columns and filters should be executed and the result will be exported to the Excel.
I am honest, I am not that skilled in VBA. What would be the best point to start from.
Your question is way too broad and as such it should not even be here, but let me give you some points to look at.
First you'll need to get your data from the Excel file. If you are not skilled with VBA, then the easiest way would be opening it through VBA using an Excel.Application object and Workbooks.Open method, and get your values from your Range.
Create an SQL Select statement from your values.
Do not use just a SELECT query, make it a SELECT INTO query (a "make table query" as Access likes to call it). That way your results will be inserted into a (new) table, making it easier to output.
Export your new table to Excel. There are several ways to do this, search for DoCmd.OutputTo or DoCmd.TransferSpreadsheet.

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.

Using an Excel macro to query a spreadsheet

So I have some data in some spreadsheets and I've found that for all the macros and filtering and forumlas I've written to simplify it and narrow it down to what I want, it would have been much easier to just write some SQL against a few tables.
I guess I'm wondering: is it possible to have a macro in a workbook that queries data in some sheets and then populates another sheet with the result set? If so, how would I do it?
(It is Excel 2003)
No need for a macro for this.
Go to DATA-> Import External Data -> Import Data then basically follow the prompts. You may need to make a new data connection, (New Source at the bottom) but once connected you can write queries natively in Excel.
I'm guessing someone familiar with DBs would be able to figure it out pretty quickly. If not, here's a tutorial.
Why do you need to use a macro when you can simply query the excel file like this:
SELECT Column1, Cloumn2, Column3
FROM [SheetName$Range]
WHERE Condition
Example:
SELECT ProductID, Qty, Price
FROM [SheetName$A10:C21]
WHERE ProductID = 545