+ Reply to Thread
Results 1 to 7 of 7

Change Chart Axis based on Calculation Change

  1. #1
    Registered User
    Join Date
    07-05-2022
    Location
    U.S
    MS-Off Ver
    M365 - 2108
    Posts
    4

    Change Chart Axis based on Calculation Change

    While using a Bar Chart as a Gantt timeline I previously was using cell values to manually update the timeline axis for Min, Max, and MajorUnit - code below where cells C3, C4, and C5 were represented respectively:

    ========================================
    Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
    Select Case Target.Address
    Case "$C$4"
    ActiveSheet.ChartObjects("Chart 3").Chart.Axes(xlValue) _
    .MaximumScale = Target.Value
    Case "$C$3"
    ActiveSheet.ChartObjects("Chart 3").Chart.Axes(xlValue) _
    .MinimumScale = Target.Value
    Case "$C$5"
    ActiveSheet.ChartObjects("Chart 3").Chart.Axes(xlValue) _
    .MajorUnit = Target.Value
    Case Else
    End Select
    End Sub
    ========================================

    However, I now want to dynamically update the Min, Max, and MajorUnit by way of "calculated" updates. To do so I understand that the method needs to change since the present method is triggering off a cell change on the worksheet that is being entered manually. What would be the simplest way to modify the code based on a calculated change?

    Note the calculation is being performed on a separate worksheet that is linked to the same C3, C4, and C5 cells where the Bar Chart is present, and the result is a calendar date - e.g. 1/23/2022.

    Thank you in advance for the response!

    Version: Office 365 Excel

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,373

    Re: Change Chart Axis based on Calculation Change

    Jon Peltier is my go to expert on chart questions, and he has this page (https://peltiertech.com/chart-udf-control-axis-scale/ ) describing a UDF that automatically controls the chart axis limits. I have done both this UDF and change/calculate event procedures. I think the UDF is easier to use in the long run.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    07-05-2022
    Location
    U.S
    MS-Off Ver
    M365 - 2108
    Posts
    4

    Re: Change Chart Axis based on Calculation Change

    Right, I assessed the Jon Peltier method of setting up a UDF (function) yet the example only indexes the month - e.g. 2 equals Feb, 3 equals Mar, etc. The Gantt chart is user selectable and can range from months to over several years, so indexing by months alone would not resolve the need. Thanks for the example.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,373

    Re: Change Chart Axis based on Calculation Change

    I'd probably have to see that in an actual example, because my experience with Peltier's UDF is that I can enter any number I want (and remember that dates are merely numbers with formatting applied to them) for axis limits for a value axis. The only reason, off the top of my head, I can think of for being limited to small integers is perhaps you are trying to modify a text/category axis, and that is what is limiting your modifications. Can you help us understand why you are limited to small integers on the axis you are trying to modify?

  5. #5
    Registered User
    Join Date
    07-05-2022
    Location
    U.S
    MS-Off Ver
    M365 - 2108
    Posts
    4

    Re: Change Chart Axis based on Calculation Change

    The UDF in the example is called as PT_ScaleChartAxis(SheetName,ChartName,X_or_Y,Primary_or_Secondary,Minimum,Maximum,MajorUnit,MinorUnit).

    So if for example the function called is: =PT_ScaleChartAxis("Timeline","Chart 10","X","Primary",C4,C5,C6,C7), where C4 is 1/1/2023, C5 is 12/31/2027, C6 is 365, C7 is 1 results in "Cannot scale a category-type axis". Of course converting the date in to its equivalent number format might work, but then to the user it does not look like a date. Perhaps I'm missing something as the UDF is a direct copy paste and the function appears to be triggering correctly.

    For clarification, the chart is rendering the dates on the bar chart grid correctly - e.g. 1/1/2023 format.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,373

    Re: Change Chart Axis based on Calculation Change

    That error suggests you are trying to change the max and min of the category (vertical axis, if this is a bar "gantt" chart and not a column chart). A category axis does not know how to make sense of numbers, so there really is no max or min when working with a category axis.

    Details about the chart? You've called it a bar (gantt) chart. Can we correctly assume that it is a bar chart and not a column chart? If I make that assumption, then the "error" that stands out to me is that you have asked the UDF to format the "x" (xlcategory which is the vertical axis in a bar chart). I would have expected to format the "y" axis (the horizontal axis in a bar chart that is an xlvalue axis). You haven't provided a sample for testing, but I'm wondering if it is a simple misunderstanding of which axis is the "x" and which is the "y" when working with a bar chart. Try putting "Y" in the 3rd argument of the UDF and see if that resolves the error?

    The other possibility I'm wondering about is if your dates are actual numbers or if you are calculating them as text strings.

  7. #7
    Registered User
    Join Date
    07-05-2022
    Location
    U.S
    MS-Off Ver
    M365 - 2108
    Posts
    4

    Re: Change Chart Axis based on Calculation Change

    You are spot on! Selecting the "Y" axis, the horizontal axis renders the bar chart correctly, even with date values as inputs, or calculated values. Of course I now feel silly and should have caught the bar chart being a rotated column chart making the axis change - Doh!

    All hail the Guru :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Change Vertical Axis on Chart based on the Chart Name
    By gruebz1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-01-2022, 04:12 AM
  2. [SOLVED] Using VBA to change axis on a chart
    By raza_m33hdy in forum Excel General
    Replies: 2
    Last Post: 01-17-2020, 03:17 PM
  3. Stacked Chart - Change Bar Colors Based on Date (X-Axis)
    By JazzLover in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 07-10-2015, 12:12 PM
  4. negative y axis on pivot chart but axis doesn't change
    By ammartino44 in forum Excel General
    Replies: 1
    Last Post: 06-24-2015, 02:19 PM
  5. [SOLVED] Change the limits of the X axis of a chart based on a cell value
    By DPWM in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-27-2013, 09:13 AM
  6. [SOLVED] Dashboard help needed. Need chart axis to change based on date ranges.
    By mcranda in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-18-2013, 10:30 AM
  7. [SOLVED] How do I change X-Axis values in a chart with 2 Y-Axis?
    By ESGLCC in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 05-04-2005, 10:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1