Tring to declare a Pivot Chart Variable

Kurt

Well-known Member
Joined
Jul 23, 2002
Messages
1,664
In the following code I am trying to declare a Chart variable named myChart.

Do I need to declare a Dim Statement at the top of the sub routine or d I need to do something else?

VBA Code:
Sub Create_Pivot_Chart_Month_And_Names_3()
'
    Sheets("2").Select
    Range("Table9[#All]").Select
    Selection.Copy
    Sheets("5b").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Table9", Version:=6).CreatePivotTable TableDestination:="5b!R1C6", _
        TableName:="myPivotTable", DefaultVersion:=6
    Sheets("5b").Select
    Cells(1, 6).Select
    With ActiveSheet.PivotTables("myPivotTable")
        .ColumnGrand = True
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = True
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 1
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlCompactRow
    End With
    With ActiveSheet.PivotTables("myPivotTable").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("myPivotTable").RepeatAllLabels xlRepeatLabels
    ActiveWorkbook.ShowPivotTableFieldList = True
    With ActiveSheet.PivotTables("myPivotTable").PivotFields("Name")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("myPivotTable").PivotFields("DOB")
        .Orientation = xlRowField
        .Position = 2
    End With
    ActiveSheet.PivotTables("myPivotTable").PivotFields("DOB").AutoGroup
    ActiveSheet.PivotTables("myPivotTable").PivotFields("Years").Orientation = _
        xlHidden
    ActiveSheet.PivotTables("myPivotTable").PivotFields("Quarters").Orientation = _
        xlHidden
    ActiveSheet.PivotTables("myPivotTable").AddDataField ActiveSheet.PivotTables( _
        "myPivotTable").PivotFields("DOB"), "Count of DOB", xlCount
    Range("F2").Select
    ActiveSheet.PivotTables("myPivotTable").PivotFields("Name").PivotFilters.Add2 _
        Type:=xlValueIsGreaterThanOrEqualTo, DataField:=ActiveSheet.PivotTables( _
        "myPivotTable").PivotFields("Count of DOB"), Value1:=2
    Range("F3").Select
    ActiveSheet.PivotTables("myPivotTable").PivotFields("DOB").PivotFilters.Add2 _
        Type:=xlValueIsGreaterThanOrEqualTo, DataField:=ActiveSheet.PivotTables( _
        "myPivotTable").PivotFields("Count of DOB"), Value1:=2
    ActiveSheet.Shapes.AddChart2(251, xlPie).Select
    ActiveChart.SetSourceData Source:=Range("'5b'!$F$1:$G$12")
    ActiveWorkbook.ShowPivotTableFieldList = False
    'ActiveSheet.Shapes("Chart 7").IncrementLeft 372.75
    ActiveSheet.Shapes("myChart").IncrementLeft 372.75
    'ActiveSheet.Shapes("Chart 7").IncrementTop -54.75
    ActiveSheet.Shapes("myChart").IncrementTop -54.75
    ActiveChart.FullSeriesCollection(1).Select
    ActiveChart.FullSeriesCollection(1).ApplyDataLabels
    ActiveChart.FullSeriesCollection(1).DataLabels.Select
    Selection.ShowPercentage = True
    Selection.ShowCategoryName = True
    Application.CommandBars("Format Object").Visible = False
    ActiveChart.ChartArea.Select
    ActiveChart.ChartTitle.Select
    Application.CommandBars("Format Object").Visible = False
    Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue
    ActiveChart.ChartTitle.Text = "Names Occuring More Than Once" & Chr(13) & "In The Same Month"
    Selection.Format.TextFrame2.TextRange.Characters.Text = _
        "Names Occuring More Than Once" & Chr(13) & "In The Same Month"
    With Selection.Format.TextFrame2.TextRange.Characters(1, 30).ParagraphFormat
        .TextDirection = msoTextDirectionLeftToRight
        .Alignment = msoAlignCenter
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(1, 5).Font
        .BaselineOffset = 0
        .Bold = msoFalse
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(89, 89, 89)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 14
        .Italic = msoFalse
        .Kerning = 12
        .Name = "+mn-lt"
        .UnderlineStyle = msoNoUnderline
        .Spacing = 0
        .Strike = msoNoStrike
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(6, 25).Font
        .BaselineOffset = 0
        .Bold = msoFalse
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(89, 89, 89)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 14
        .Italic = msoFalse
        .Kerning = 12
        .Name = "+mn-lt"
        .UnderlineStyle = msoNoUnderline
        .Spacing = 0
        .Strike = msoNoStrike
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(31, 17).ParagraphFormat
        .TextDirection = msoTextDirectionLeftToRight
        .Alignment = msoAlignCenter
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(31, 17).Font
        .BaselineOffset = 0
        .Bold = msoFalse
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(89, 89, 89)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 14
        .Italic = msoFalse
        .Kerning = 12
        .Name = "+mn-lt"
        .UnderlineStyle = msoNoUnderline
        .Spacing = 0
        .Strike = msoNoStrike
    End With
    ActiveChart.ChartArea.Select
    Range("E1").Select
End Sub

The code works perfectly until it reaches this line:

ActiveSheet.Shapes("myChart").IncrementLeft 372.75

It starts to create the chart and then stops.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hello



Can you post sample data showing how Table 9 looks like? I would like to run the entire code.
 
Upvote 0
Hello Worf,

Thank you for your reply.

I will just paste the data directly here:

NameDOB
Adella
12/18/1955​
Allen
8/26/1963​
Anne
10/29/1947​
Beverly
1/2/1971​
Bonnie
12/28/1961​
Brian
10/16/1979​
Carol
4/2/1971​
Christopher
11/9/1966​
Coral
11/11/1961​
Daryl
8/28/1937​
David
10/28/1956​
David
5/18/1962​
David
8/2/1962​
Deborah
7/2/1956​
Dolores
3/6/1970​
Donna
2/10/1962​
Donna
3/21/1972​
Edward
4/9/1963​
Eleanor
9/7/1958​
Eva
8/23/1972​
Frank
5/2/1945​
Fred
1/3/1960​
George
1/24/1964​
Harry
10/28/1931​
James
3/24/1939​
James
2/7/1944​
Jennifer
7/29/1958​
Jennifer
6/11/1971​
Jill
7/27/1960​
Joanne
5/24/1953​
Jody
2/2/1954​
John
2/22/1933​
John
10/7/1965​
John
4/25/1972​
Judy
6/7/1954​
Julia
10/15/1969​
Kathleen
7/8/1942​
Kathleen
7/7/1970​
Kathy
12/31/1935​
Kiley
1/29/1969​
Larry
5/4/1952​
Larry
5/13/1956​
Lewis
5/26/1966​
Lewis "Jay"
9/15/1964​
Linda
10/11/1960​
Lisa
6/11/1947​
Margaret
8/16/2004​
Marilyn
7/30/1954​
Mary
10/24/1945​
Mary
12/29/1963​
Matthew
6/25/1949​
Melvin
4/15/1933​
Nicole
12/17/1962​
Patricia
10/30/1956​
Patricia
10/30/1978​
Patty
2/18/1958​
Paul
9/6/1956​
Paul
10/19/1960​
Raymond
4/23/1950​
Richard
3/21/1944​
Richard
7/9/1955​
Richard
8/12/1961​
Richard
8/23/1965​
Robert
2/27/1951​
Robert
10/24/1962​
Roy "Bud"
12/1/1944​
Shawn
3/5/1958​
Sonja
10/6/1951​
Susan
12/13/1948​
Susan
3/28/1954​
Theodore
5/16/1953​
Theodore
9/16/1974​
Thomas
12/15/1959​
William
9/12/1960​
William
9/16/1960​
 
Upvote 0
I apologize in advance I didn't know it would paste the columns that wide, but that is the data.

This data would be copied and then used in the pivot table.
 
Upvote 0
VBA Code:
Dim chvar As Chart ' this is a variable

ActiveSheet.Shapes("my Chart").IncrementLeft 372.75


I am going offline now but will return tomorrow. Note that the second code line above is referring to a chart named my Chart, the first line declares a variable.
 
Upvote 0
How about years and quarters mentioned in the code, are they members of the source table?
 
Upvote 0
Code:
Dim myChart as Chart
Set myChart = ActiveSheet.Shapes.AddChart2(251, xlPie).Chart

you can then use myChart instead of ActiveChart. Where you are trying to adjust the containing shape, use myChart.Parent
 
Upvote 0

Forum statistics

Threads
1,223,155
Messages
6,170,405
Members
452,325
Latest member
BlahQz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top