VBA Macro to create Bar Chart(Image Included)

XcelNoobster

New Member
Joined
Jun 7, 2022
Messages
40
How would I create a VBA Macro that generates a bar chart from the following sample data in Sheet Name "Final Result"?

Screenshot 2023-06-06 091757.png
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You'll need a pivot chart. I suggest you start by recording a macro while you create it.
 
Upvote 0
One way is to use the macro recorder to record your actions as you manually create the bar chart.
 
Upvote 0
You'll need a pivot chart. I suggest you start by recording a macro while you create it.
Okay so i tried recording the macro, but get errors in the macro whenever i try to re-run the macro. What if there is more data than the sample above?, etc
 
Upvote 0
How about posting the macro recorder VBA code?
 
Upvote 0
The pivot chart is what i am looking for, but
How about posting the macro recorder VBA code?
VBA Code:
Sub Record()
'
' Record Macro
'

'
    Windows("Book2").Activate
    ActiveCell.Offset(-13, -6).Range("A1:C7").Select
    ActiveCell.Offset(-12, -6).Range("A1").Activate
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R7C3", Version:=8).CreatePivotTable TableDestination:= _
        "Sheet2!R1C1", TableName:="PivotTable28", DefaultVersion:=8
    Sheets("Sheet2").Select
    Cells(1, 1).Select
    With ActiveSheet.PivotTables("PivotTable28")
        .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("PivotTable28").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("PivotTable28").RepeatAllLabels xlRepeatLabels
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Range("Sheet2!$A$1:$C$18")
    ActiveSheet.Shapes("Chart 1").IncrementLeft 192
    ActiveSheet.Shapes("Chart 1").IncrementTop 14.5
    With ActiveChart.PivotLayout.PivotTable.PivotFields("Order")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
        PivotTable.PivotFields("ITEM"), "Count of ITEM", xlCount
    Windows("SearchMacro.xlsm").Activate
End Sub
 
Upvote 0
After commenting out a few irrelevant lines, your recorder code appears to draw the chart you want.
VBA Code:
Sub Record()
'
' Record Macro
'

'
    'Windows("Book2").Activate
    'ActiveCell.Offset(-13, -6).Range("A1:C7").Select
    'ActiveCell.Offset(-12, -6).Range("A1").Activate
    
   With ActiveSheet
   .Range("A1:C7").Select
   .Range("A1").Activate
   End With
    
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R7C3", Version:=8).CreatePivotTable TableDestination:= _
        "Sheet2!R1C1", TableName:="PivotTable28", DefaultVersion:=8
    Sheets("Sheet2").Select
    Cells(1, 1).Select
    With ActiveSheet.PivotTables("PivotTable28")
        .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("PivotTable28").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("PivotTable28").RepeatAllLabels xlRepeatLabels
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Range("Sheet2!$A$1:$C$18")
    ActiveSheet.Shapes("Chart 1").IncrementLeft 192
    ActiveSheet.Shapes("Chart 1").IncrementTop 14.5
    With ActiveChart.PivotLayout.PivotTable.PivotFields("Order")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
        PivotTable.PivotFields("ITEM"), "Count of ITEM", xlCount
    'Windows("SearchMacro.xlsm").Activate
End Sub

[ATTACH type="full"]93060[/ATTACH]
 

Attachments

  • 1686092801156.png
    1686092801156.png
    18.2 KB · Views: 4
Upvote 0

Forum statistics

Threads
1,215,156
Messages
6,123,339
Members
449,098
Latest member
thnirmitha

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