Chart Template Based On Data Validation

jakeenos

New Member
Joined
Dec 29, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

Firstly, I'd like to thank the people that have provided solutions to threads on this website. It has been incredibly beneficial to me.

So, here is my issue. I'm looking for a way to have a chart change based upon data validation. The change would be from one chart template to another chart template.

I have a report that needs the same charts, but they need to be formatted for a white background and a black background.

I have a template for each scenario, but I would like to have some way to have them change without having to manually update each one by going through the 'Change Chart Type' window.

I figured this could be solved with data validation, but it may be the case that a macro would be more applicable.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Lots of detail missing, but I hope the following demo will put you on the right track.
Assumptions:
The data validation cell on your sheet is B1 (change to suit).
The chart you want to change is the first chart on the sheet (index 1 - change to suit).
The 2 templates have the same names as the data validation choices - i.e. when you select "Chart1" in B1 then the template you have named "Chart1" is used, likewise with Chart2.
The path to you chart templates will have to be changed to match your actual template locations.

So with all of the above taken into account, put the following worksheet change event code in the sheet module of the sheet with the chart in question (right-click the sheet tab name, select View Code & put the code in the window that appears). You'll need to save the file in macro-enabled or binary format for it to work.
The code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("B1"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Dim myChart As ChartObject, Temp As String
        Set myChart = ActiveSheet.ChartObjects(1)
        Temp = [B1].Value
        ActiveSheet.ChartObjects(1).Select
        ActiveChart.ApplyChartTemplate ("C:\Users\kevin\AppData\Roaming\Microsoft\Templates\Charts\" & Temp & ".crtx")
        [B1].Select
    End If
    
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub

When Chart1 is selected in B1 you get this:
Picture1.png

When Chart2 is selected in B1 you get this:
Picture2.png
 
Upvote 1
Solution
Code slimmed down a bit:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("B1"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        ActiveSheet.ChartObjects(1).Select
        ActiveChart.ApplyChartTemplate ("C:\Users\kevin\AppData\Roaming\Microsoft\Templates\Charts\" & [B1] & ".crtx")
        [B1].Select
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
 
Upvote 0
Code slimmed down a bit:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("B1"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        ActiveSheet.ChartObjects(1).Select
        ActiveChart.ApplyChartTemplate ("C:\Users\kevin\AppData\Roaming\Microsoft\Templates\Charts\" & [B1] & ".crtx")
        [B1].Select
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
Hey Kevin,

Thank you for your reply! I really appreciate the help. I'm a novice when it comes to marcos, so you'll have to excuse my confusion.

I've provided a picture to showcase your code that I've adjusted as well as the sheet in question.

I've placed the worksheet change event code in the sheet module of the sheet with the chart in question.

I've changed the names of the chart templates to match the data validation list in cell B20.

I've saved the file as a macro-enabled file.

This code works great for the first chart! Is there some adjustment that can be made to have this work for two charts? Each tab that I need this for has two charts.

1704147197980.png



1704146710452.png
 
Upvote 0
Code slimmed down a bit:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("B1"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        ActiveSheet.ChartObjects(1).Select
        ActiveChart.ApplyChartTemplate ("C:\Users\kevin\AppData\Roaming\Microsoft\Templates\Charts\" & [B1] & ".crtx")
        [B1].Select
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
Hey Kevin,

Good news! I was able to adjust the code to make it work for both. Thank you so much for your help!
 
Upvote 0
Happy to hear you got it working, it's always more satisfying when we work it out for ourselves. Welcome to the forum, and thanks for the feedback 👍
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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