VBA to automatically adjust my Y-Axis

yad

New Member
Joined
Oct 6, 2021
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
i have a code that adjust the Y-axis of my chart but it applies that to all the chart and i only need it to be applied on chart 8 . help please .
here is the code :

Sub AdjustVerticalAxis()
Dim cht As ChartObject
Dim srs As Series
Dim FirstTime As Boolean
Dim MaxNumber As Double
Dim MinNumber As Double
Dim MaxChartNumber As Double
Dim MinChartNumber As Double
Dim Padding As Double


Padding = 0.1


Application.ScreenUpdating = False


For Each cht In ActiveSheet.ChartObjects


FirstTime = True


For Each srs In cht.Chart.SeriesCollection

MaxNumber = Application.WorksheetFunction.Max(srs.Values)


If FirstTime = True Then
MaxChartNumber = MaxNumber
ElseIf MaxNumber > MaxChartNumber Then
MaxChartNumber = MaxNumber
End If


MinNumber = Application.WorksheetFunction.Min(srs.Values)


If FirstTime = True Then
MinChartNumber = MinNumber
ElseIf MinNumber < MinChartNumber Or MinChartNumber = 0 Then
MinChartNumber = MinNumber
End If


FirstTime = False
Next srs


cht.Chart.Axes(xlValue).MinimumScale = MinChartNumber * (1 - Padding)
cht.Chart.Axes(xlValue).MaximumScale = MaxChartNumber * (1 + Padding)

Next cht

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
One way

VBA Code:
Sub AdjustVerticalAxis()
    Dim cht As ChartObject
    Dim srs As Series
    Dim FirstTime As Boolean
    Dim MaxNumber As Double
    Dim MinNumber As Double
    Dim MaxChartNumber As Double
    Dim MinChartNumber As Double
    Dim Padding As Double
    Dim ChartName As String
    
    ChartName = "Chart 8" 'chart to process
    
    On Error Resume Next
    Set cht = ActiveSheet.ChartObjects(ChartName)
    On Error GoTo 0
    
    If Not cht Is Nothing Then
        Padding = 0.1
        Application.ScreenUpdating = False
        FirstTime = True
        
        For Each srs In cht.Chart.SeriesCollection
            MaxNumber = Application.WorksheetFunction.Max(srs.Values)
            
            If FirstTime = True Then
                MaxChartNumber = MaxNumber
            ElseIf MaxNumber > MaxChartNumber Then
                MaxChartNumber = MaxNumber
            End If
            
            MinNumber = Application.WorksheetFunction.Min(srs.Values)
            
            If FirstTime = True Then
                MinChartNumber = MinNumber
            ElseIf MinNumber < MinChartNumber Or MinChartNumber = 0 Then
                MinChartNumber = MinNumber
            End If
            
            FirstTime = False
        Next srs
        
        cht.Chart.Axes(xlValue).MinimumScale = MinChartNumber * (1 - Padding)
        cht.Chart.Axes(xlValue).MaximumScale = MaxChartNumber * (1 + Padding)
        Application.ScreenUpdating = True
    Else
        MsgBox "Cannot find " & ChartName
    End If
End Sub
 
Upvote 0
Solution
One way

VBA Code:
Sub AdjustVerticalAxis()
    Dim cht As ChartObject
    Dim srs As Series
    Dim FirstTime As Boolean
    Dim MaxNumber As Double
    Dim MinNumber As Double
    Dim MaxChartNumber As Double
    Dim MinChartNumber As Double
    Dim Padding As Double
    Dim ChartName As String
   
    ChartName = "Chart 8" 'chart to process
   
    On Error Resume Next
    Set cht = ActiveSheet.ChartObjects(ChartName)
    On Error GoTo 0
   
    If Not cht Is Nothing Then
        Padding = 0.1
        Application.ScreenUpdating = False
        FirstTime = True
       
        For Each srs In cht.Chart.SeriesCollection
            MaxNumber = Application.WorksheetFunction.Max(srs.Values)
           
            If FirstTime = True Then
                MaxChartNumber = MaxNumber
            ElseIf MaxNumber > MaxChartNumber Then
                MaxChartNumber = MaxNumber
            End If
           
            MinNumber = Application.WorksheetFunction.Min(srs.Values)
           
            If FirstTime = True Then
                MinChartNumber = MinNumber
            ElseIf MinNumber < MinChartNumber Or MinChartNumber = 0 Then
                MinChartNumber = MinNumber
            End If
           
            FirstTime = False
        Next srs
       
        cht.Chart.Axes(xlValue).MinimumScale = MinChartNumber * (1 - Padding)
        cht.Chart.Axes(xlValue).MaximumScale = MaxChartNumber * (1 + Padding)
        Application.ScreenUpdating = True
    Else
        MsgBox "Cannot find " & ChartName
    End If
End Sub
Hello again, the code worked great , i'd like to know if there is a way to make it function without having to run the macro everytime ?
Thank you
 
Upvote 0
That's a different problem from applying the code to a different chart. You should start a new thread for the new problem.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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