473,769 Members | 6,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create Pivot Table using VBA (Access 2007)

34 New Member
I have produced an Excel (2007) worksheet, with which I want to create a Pivot Table. This would all be done from a module run on an Access 2007 database.

Please can anyone supply some simple code for this?

Many thanks for any help received.
Apr 21 '09 #1
32 23395
NeoPa
32,573 Recognized Expert Moderator MVP
I'm afraid not.

We are here to assist people to develop their own work, not to do the work for them. There's flexibility to this, but in a case such as this, where it could easily be a homework or project question, we would be very hesitant to provide anything.

Please see the Posting Guidelines for how you could redo this in such a way that we can help.
Apr 21 '09 #2
redman08
34 New Member
This is not homework, nor a project question.

I have come across various bits of code on the net and looked at output from a macro recording when creating a pivot table in escel (2007).

However, when transferring this to a vba module, I keep getting errors. In a vain attempt to see if there was a quick and simple resolution to this problem, I raised this post.

I have seen far simpler (I know, in my opinion) problems resolved in this forum, so it seemed right to ask here.
Apr 21 '09 #3
NeoPa
32,573 Recognized Expert Moderator MVP
I'm certainly not saying you can't ask for help. Nor that the problem is too complex.

Please reread the post, with specific reference to the linked Help section that tells how questions should be asked. Posting what you have, with an explanation of what's failing where, would be acceptable. There is nothing to stop me, or anyone else, providing better code at that stage if that seems appropriate.

None of us is looking to make it hard to ask questions (why would we be here for that?), but the link does explain what we expect in the way of asking questions, and alo why if you're interested to look that deeply.
Apr 21 '09 #4
redman08
34 New Member
The current code looks like this, where Datasheet is the worksheet containing the data, and CurrentSheet is a new sheet being created to hold the Pivot Table:
Expand|Select|Wrap|Line Numbers
  1. With AppExcel.ActiveWorkbook.PivotCaches
  2.     With .Add(SourceType:=xlDatabase, SourceData:=Datasheet.Range("A1:H" & LastRowNumber), Version:=xlPivotTableVersion12)
  3.         Set PTable = .CreatePivotTable(TableDestination:=CurrentSheet.Range("A6"), TableName:="AnalysisPivot", DefaultVersion:=xlPivotTableVersion12)
  4.     End With
  5. End With
I have tried various versions of the above including holding the data/table output ranges in named variables.

Currently, this code returns a "Run-time error '448':Named argument not found" error message.
Apr 21 '09 #5
NeoPa
32,573 Recognized Expert Moderator MVP
Now you're talking.

I will see if I can help some here, but I'm not experienced with Pivot Tables. Probably later though as I'm working just now.

BTW. Can you say which line of the code the error message came on? It'll be either #2 or #3 I would guess, but that can tie down where to look.
Apr 21 '09 #6
redman08
34 New Member
The error is on Line #2
Apr 21 '09 #7
FishVal
2,653 Recognized Expert Specialist
Frankly speaking, the code looks fine.
Maybe Excel doesn't like the range you make PivotCache from.
I've made some trials and found that CreatePivotTabl e method fails if first cell in the range is empty.
Apr 21 '09 #8
FishVal
2,653 Recognized Expert Specialist
@redman08
what value is stored in LastRowNumber variable at this point?
Apr 21 '09 #9
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Beware! The macro recorder produces code that runs fine inside an Excel workbook where the Application object is implied. In an Excel sheet you can refer to ActiveSheet, ActiveWorkbook and so on without specifying the application object explicitly. However, references to implied Excel application objects do not work when running code in an Access VBA module using Access to communicate with Excel as an automation server.

For instance, the macro recorder frequently produces code where the ActiveSheet object is implied, so you will find things like

Range("A1:A1"). <Do Something>

which fail in Access VBA, as there is no implicit Application.Som eWorksheet object wrapping the code to link back to the specified Range object.

You will need to fully qualify implicit references to tie them to the appropriate Application object, as you are doing in the first part of the With on line 1, and ensure that all object variables you use (such as DataSheet and CurrentSheet) are explicitly set:

Expand|Select|Wrap|Line Numbers
  1. ...SourceData:=Datasheet.Range("A1:H" & LastRowNumber)...
  2. ...TableDestination:=CurrentSheet.Range("A6")...
You will also need to check that the xlDatabase and xlPivotTableVer sion12 constants are available within your Access VBA code module - the debugger can help you here - as if they are not you will need to substitute their actual values for the symbolic ones listed (or supply equivalent constants of your own).

I would expect to see lines like this to set the value of Datasheet, for example:

Expand|Select|Wrap|Line Numbers
  1. Dim DataSheet as WorkSheet
  2. Set DataSheet = AppExcel.ActiveWorkBook.Sheets("DataSheet")
  3. ...
If you have not set Datasheet as a worksheet object you cannot treat it as such in line 2.

-Stewart
Apr 21 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
5844
by: Rob | last post by:
I'm just getting around to using pivot tables and charts. I find the Pivot table interface to be INCREDIBLY frustrating. When I view a table in Design view, then choose Pivot table view, I get the PivotTable Field list, from which I can choose to add fields to the view. 1. How do you get RID of a field once you put it in? I can not right click on anywhere in the view and have ANY of the submenu options be availble. From the PivotTable...
4
11694
by: Ajay | last post by:
Hello all, I used to display reports in Excel earlier on my website. Now the client has requested that he would like see reports in Pivot table on the web . The backend is Sql2000. Can you please help in determining hoe we can create a pivot table and display records in it using C#? Thanks Ajay
1
3280
by: peter | last post by:
Dear all, I have an existing query called 'A', but I want it to view in Pivot Table. What I do is : - Double click the query and choose Pivot Table view. - I make some changes by adding some fields to the pivot table. - Next, I want to save these setting as a new query. (Because I know that multiple templates may be needed to keep track as user has change the
9
6797
by: PeteCresswell | last post by:
I've got something called "Reference Rates". The idea is that on a given day, we have various rates of return for various entities. e.g. Libor 3-month return, Libor 6-month return, US Treasury Bonds, the Prime rate, and so-forth. We associate a security with one of those rates. There are a set of rates for each calendar day, and the rates for that
2
13528
by: LittlePhil via AccessMonster.com | last post by:
Someone please help before i start to cry. I'm trying to export from Access to Excel, then create a new excel sheet with a pivot table to display the data held in columns A:P. I get the error message "Run Time error 91: Object variable or with block variable not set" on the "CREATE PIVOT" line below and can't find a way round. Please someone. Help! Option Compare Database
18
4526
by: smckibbe01 | last post by:
I have Pivot Table query in MS Access that I would like to run in SQL Server 2005. Can anyone help me to convert the syntax over to SQL Server. The following is the query in MS Access: TRANSFORM SUM(ps.psScheduled) SELECT pi.piMaxTurns, pi.piAMP, pi.piTons, '', '', ps.psPipe
0
1058
by: Sport Girl | last post by:
Hello everybody, please can anybody help me: How to make in a Pivot Table designed in Access 2007 a link from each field in the Pivot table to the corresponding row in the table in the database. This tip we can use it in Excel. Does anybody ahs an idea?
1
2204
by: mld01s | last post by:
I really need help!!! I dont know if its possible to share pivot tables, or see pivot tables in other machines that the one where the tables were created. This is what happens: I created a database to track different daily results. In a form in that database I added a pivot table, this table lets me see the results, sort them, and everthing a pivot table should let users do. Below the pivot table on the same form I have some fields for...
0
2138
by: Clare CAVS | last post by:
I have a table with a lookup column referring to another table . tblRooms has two fields, (Autonumber), and . The column I want to display is the RoomName column. If I have Bound Column = 1, and Column Count = 2, Column Widths 0cm;3cm I get the display I want. However, if I change the Bound Column to 2, this changes the values displayed – I now get column 1 values (RoomID) displaying, regardless of what I set for the Column Count and...
0
9590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.