VBA to set Chart MajorUnit to specific cell

kmbryant09

New Member
Joined
Feb 16, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have a Chart ("Chart 3") that has some basic VBA code attached to it to set the Y-Axis Min & Max values based on specific cells:

ActiveSheet.ChartObjects("Chart 3").Chart.Axes(xlValue).MaximumScale = Range("CB1").Value
ActiveSheet.ChartObjects("Chart 3").Chart.Axes(xlValue).MinimumScale = Range("CB2").Value

I'm trying to also add VBA to set the Y-Axis Major Unit to cell CB3. I've tried the following code, but get an error when running the Macro:

ActiveSheet.ChartObjects("Chart 3").Chart.Axes(xlValue).MajorUnit = Range("CB3").Value

Any help?

Thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Did you get an answer? I am trying to do exactly the same thing.
 
Upvote 0
I setteled on this. works for me
Function setChartAxis(sheetName As String, chartName As String, MinOrMax As String, _
ValueOrCategory As String, PrimaryOrSecondary As String, Value As Variant)

'Create variables
Dim cht As Chart
Dim valueAsText As String

'Set the chart to be controlled by the function
Set cht = Application.Caller.Parent.Parent.Sheets(sheetName) _
.ChartObjects(chartName).Chart

'Set Value of Primary axis
If (ValueOrCategory = "Value" Or ValueOrCategory = "Y") _
And PrimaryOrSecondary = "Primary" Then

With cht.Axes(xlValue, xlPrimary)
If IsNumeric(Value) = True Then
If MinOrMax = "Max" Then .MaximumScale = Value
If MinOrMax = "Min" Then .MinimumScale = Value
If MinOrMax = "Major" Then .MajorUnit = Value
Else
If MinOrMax = "Max" Then .MaximumScaleIsAuto = True
If MinOrMax = "Min" Then .MinimumScaleIsAuto = True
If MinOrMax = "Major" Then .MajorUnitIsAuto = True
End If
End With
End If

'Set Category of Primary axis
If (ValueOrCategory = "Category" Or ValueOrCategory = "X") _
And PrimaryOrSecondary = "Primary" Then

With cht.Axes(xlCategory, xlPrimary)
If IsNumeric(Value) = True Then
If MinOrMax = "Max" Then .MaximumScale = Value
If MinOrMax = "Min" Then .MinimumScale = Value
If MinOrMax = "Major" Then .MajorUnit = Value
Else
If MinOrMax = "Max" Then .MaximumScaleIsAuto = True
If MinOrMax = "Min" Then .MinimumScaleIsAuto = True
If MinOrMax = "Major" Then .MajorUnitIsAuto = True
End If
End With
End If

'Set value of secondary axis
If (ValueOrCategory = "Value" Or ValueOrCategory = "Y") _
And PrimaryOrSecondary = "Secondary" Then

With cht.Axes(xlValue, xlSecondary)
If IsNumeric(Value) = True Then
If MinOrMax = "Max" Then .MaximumScale = Value
If MinOrMax = "Min" Then .MinimumScale = Value
If MinOrMax = "Major" Then .MajorUnit = Value
Else
If MinOrMax = "Max" Then .MaximumScaleIsAuto = True
If MinOrMax = "Min" Then .MinimumScaleIsAuto = True
If MinOrMax = "Major" Then .MajorUnitIsAuto = True
End If
End With
End If

'Set category of secondary axis
If (ValueOrCategory = "Category" Or ValueOrCategory = "X") _
And PrimaryOrSecondary = "Secondary" Then
With cht.Axes(xlCategory, xlSecondary)
If IsNumeric(Value) = True Then
If MinOrMax = "Max" Then .MaximumScale = Value
If MinOrMax = "Min" Then .MinimumScale = Value
If MinOrMax = "Major" Then .MajorUnit = Value
Else
If MinOrMax = "Max" Then .MaximumScaleIsAuto = True
If MinOrMax = "Min" Then .MinimumScaleIsAuto = True
If MinOrMax = "Major" Then .MajorUnitIsAuto = True
End If
End With
End If

'If is text always display "Auto"
If IsNumeric(Value) Then valueAsText = Value Else valueAsText = "Auto"

'Output a text string to indicate the value
setChartAxis = ValueOrCategory & " " & PrimaryOrSecondary & " " _
& MinOrMax & ": " & valueAsText

End Function
 

Attachments

  • Capture.JPG
    Capture.JPG
    38.6 KB · Views: 15
Upvote 0
Oops, wrong image. It looks like the image here. The Right column cell formula for X axis Major unit is: =setChartAxis("EXCH vs LQA","Chart 3","Major","X","Primary",VALUE(AA99)) where AA is the middle coulumn.
 

Attachments

  • Capture.JPG
    Capture.JPG
    48.7 KB · Views: 6
Upvote 0

Forum statistics

Threads
1,224,826
Messages
6,181,192
Members
453,021
Latest member
pingpong7117

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