Solved

VBA Please

Posted on 2013-01-30
4
207 Views
Last Modified: 2013-02-01
There is a large Named Range (“Source”) in a excel file. Need to VBA to do this:
a.      Without removing or deleting the named range , add a table to the named range so that it gives some good format to the 30000 rows.
b.      Create a pivot table out of that named range’s range in the format  shown in ‘pivot’ sheet
Copy-of-pivotV2--VBA.xlsx
0
Comment
Question by:Rayne
  • 2
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
redmondb earned 500 total points
ID: 38837782
Hi, Rayne.

Please try this code...
Option Explicit

Sub Build_Name_and_Pivot()
Dim xSource    As Worksheet
Dim xPivot     As Worksheet
Dim xTableName As String

Set xSource = Sheets("source")
Application.Goto Reference:="Source"

On Error Resume Next
    Selection.ListObject.Unlist
On Error GoTo 0

xTableName = "Table1"

xSource.ListObjects.Add(xlSrcRange, Range([Source].Address), , xlYes).Name = xTableName
Range(xTableName & "[#All]").Select

Set xPivot = Sheets.Add

ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=xTableName _
    , Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=xPivot.Name & "!R3C1" _
    , TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion14
    
With xPivot.PivotTables("PivotTable2")
    .AddDataField xPivot.PivotTables( _
        "PivotTable2").PivotFields("GRPAllocated$"), "Sum of GRPAllocated$", xlSum
    .AddDataField xPivot.PivotTables( _
        "PivotTable2").PivotFields("GRPAllocated$Changed"), _
        "Sum of GRPAllocated$Changed", xlSum
        
    With .PivotFields("GRP")
        .Orientation = xlRowField
        .Position = 1
    End With

    With .PivotFields("Parts")
        .Orientation = xlRowField
        .Position = 2
    End With

End With

End Sub

Open in new window

Regards,
Brian.
0
 

Author Comment

by:Rayne
ID: 38838322
Perfect Brian,

You are awesome :)
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38839634
Thanks, Rayne. Glad to help!
0
 

Author Comment

by:Rayne
ID: 38842991
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now