2

I created a VBA script that creates a chart and places it on a separate workbook. To create this chart I have data (pivot table) in different worksheets from another workbook. I used that data to create chart and placed into a different workbook. What I need is to also add a slicer from the same pivot table to use with the chart. Once I get the slicer on the same workbook as the chart I need to be able to click each button on the slicer for the chart to change from the pivot table.

I'm not that familiar with slicers and I haven't come across any examples on how to connect the slicer with the chart.

My code is below:

Sub create_chart_sheet()
    'get the range for the source data
    Dim sourceDataRange As Range
    Dim wk As Worksheet
    
    
'    create a new workbook that contains a single blank worksheet
    Dim destinationWorkbook As Workbook
    Set destinationWorkbook = Application.Workbooks.Add
    Set wk = destinationWorkbook.Worksheets(1)
    
    Set src = Workbooks.Open("C:\Users\username\Desktop\test.xlsx", True, True)
    Set sourceDataRange = src.Worksheets("worksheet").Range("B1:105")
    
    'create a new chart sheet and place it at the beginning of the destination workbook
    Dim oChartSheet As Chart
    
'    Set oChartSheet = destinationWorkbook.Charts.Add(destinationWorkbook.Sheets(1))
    Set oChartSheet = wk.Shapes.AddChart.Chart
 
    
    'set the properties for the chart
    With oChartSheet
        .SetSourceData Source:=sourceDataRange
        .ChartTitle.text = "Title of Chart"
        .ChartType = xlLine
        .Axes(xlValue).TickLabels.NumberFormat = "0.0"
        .Axes(xlValue).TickLabels.NumberFormat = "#,##0.0"
'        .Axes(xlCategory).MajorUnit = 2
'        .Axes(xlCategory).MajorUnitScale = xlMonths
        
        .ChartArea.Top = Range("P2:Y20").Top
        .ChartArea.Left = Range("P2:Y20").Left
       
    End With
            
End Sub

0 Answers0