2d & 3d charts present

Gordon Smith

New Member
Joined
May 31, 2012
Messages
2
I have both 2D and 3D stacked bar charts working from identical source data with identical horizontal axis format options set:
Min = Auto
Max = Auto
Major unit = Fixed: 0.3
Minor unit = Fixed: 0.3

Unfortunately the horizontal axis of the 2D chart is longer and has whitespace at the end of the plotted data (where 9.3 & 9.6 are), which I'd really like to eliminate.

The data is constantly changing so I cannot use a fixed value for the range maximum.

I cannot use a 3D chart as they do not support secondary axes and I wasn't able to get the vertical gridlines to line up very well.

33k7h1j.png

(the 2D chart is at the bottom)
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Welcome to the Board!

This code will set the max Horizontal value of all 2D or 3D bar charts on the activesheet, based on the max row sum of the cells in rows 2 and 3.

Code:
Option Explicit

Sub SetHorizontalAxisMaxValue()

    Dim chtobj As ChartObject
    Dim sngMaxSum As Single
    Dim sngRowSum As Single
    Dim lX As Long
    
    With ActiveSheet
        'Find max value of summed row
        For lX = 2 To 3
            sngRowSum = Application.WorksheetFunction.Sum(.Rows(lX))
            If sngRowSum > sngMaxSum Then sngMaxSum = sngRowSum
        Next
        
        'Set max Horizontal to that value
        For Each chtobj In .ChartObjects
            Select Case chtobj.Chart.ChartType  'xlChartType
            Case xl3DBarStacked, xlBarStacked   '61, 58
                With chtobj.Chart
                    .Axes(xlValue).MaximumScale = sngMaxSum
                End With
            End Select
        Next
    End With
    
End Sub
 
Upvote 0
This works very well, thank you for coding a solution.

Is there any way to accomplish this without using a macro?
I only ask in case there is a way to make this possible without showing my users a security warning.
 
Upvote 0
I don't know of a way to do it without the macro.
Have the users put the the excel file in a trusted location or or use the trust center to define its current location as trusted. If you are on a network that uses authentication, you can digitally sign the code (from the VBE editor Tools | Digital Signature) which should allow the users to identify your code as safe the first time they run it so they won't keep seeing the security warning.
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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