Solved

VBA Please

Posted on 2013-01-30
4
217 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

770 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