VBA to Obtain Chart Axes Major and Minor Units

bisel

Board Regular
Joined
Jan 4, 2010
Messages
223
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I am trying to use VBA to obtain the value of the major and minor units for a chart's vertical axis and then assign that value to range on the worksheet. Here is the code ...
VBA Code:
:
:
' Set major and minor units
    Sheet7.Range("chart1_major").Value = Sheet7.ChartObjects("startyear_rfb_chart").Chart.Axes(x1Value).MajorUnit
    Sheet7.Range("chart1_minor").Value = Sheet7.ChartObjects("startyear_rfb_chart").Chart.Axes(x1Value).MinorUnit
:
:

When I execute the code, I get this error message ...

1601698711014.png


Trying to debug this and running into dead end. Any help appreciated.

Thanks,

Steve
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi Steve. Your question isn't clear. U stated that you're trying to obtain the value of the major unit but your code says that you're trying to set it. Anyways, I'm not sure what part of your code is wrong but I set up a trial wb with a chart on sheet 1 and a named range "chart1_major" and all of the code below works. HTH. Dave
Code:
'display major unit value
MsgBox Sheets("Sheet1").ChartObjects(1).Chart.Axes(xlValue).MajorUnit
'1)set named range "chart1_major" to major unit value
Sheets("Sheet1").Range("chart1_major").Value = _
        Sheets("Sheet1").ChartObjects(1).Chart.Axes(xlValue).MajorUnit
'2)set major unit value to named range "chart1__major"
Sheets("Sheet1").ChartObjects(1).Chart.Axes(xlValue).MajorUnit = _
        Sheets("Sheet1").Range("chart1_major").Value
ps. use either 1 or 2 above
 
Upvote 0
Hi Steve. Your question isn't clear. U stated that you're trying to obtain the value of the major unit but your code says that you're trying to set it. Anyways, I'm not sure what part of your code is wrong but I set up a trial wb with a chart on sheet 1 and a named range "chart1_major" and all of the code below works. HTH. Dave
Code:
'display major unit value
MsgBox Sheets("Sheet1").ChartObjects(1).Chart.Axes(xlValue).MajorUnit
'1)set named range "chart1_major" to major unit value
Sheets("Sheet1").Range("chart1_major").Value = _
        Sheets("Sheet1").ChartObjects(1).Chart.Axes(xlValue).MajorUnit
'2)set major unit value to named range "chart1__major"
Sheets("Sheet1").ChartObjects(1).Chart.Axes(xlValue).MajorUnit = _
        Sheets("Sheet1").Range("chart1_major").Value
ps. use either 1 or 2 above

What the code is stating is that a named range is set to the Major or Minor Units of the charts. And I resolved this by re-writing the code. It is exactly the same, but now it works. Here is my code ...

VBA Code:
Sub charts_update()

Dim maxpct As Double
Dim axesmajor As Double
Dim axesminor As Double
:
:
' Set major and minor units
    Sheet7.Range("chart1_major").Value = Sheet7.ChartObjects("startyear_rfb_chart").Chart.Axes(xlValue).MajorUnit
    Sheet7.Range("chart2_major").Value = Sheet7.ChartObjects("eoy_reserve_chart").Chart.Axes(xlValue).MajorUnit
    Sheet7.Range("chart3_major").Value = Sheet7.ChartObjects("startyear_reserve_expenses_chart").Chart.Axes(xlValue).MajorUnit
    Sheet7.Range("chart1_minor").Value = Sheet7.ChartObjects("startyear_rfb_chart").Chart.Axes(xlValue).MinorUnit
    Sheet7.Range("chart2_minor").Value = Sheet7.ChartObjects("eoy_reserve_chart").Chart.Axes(xlValue).MinorUnit
    Sheet7.Range("chart3_minor").Value = Sheet7.ChartObjects("startyear_reserve_expenses_chart").Chart.Axes(xlValue).MinorUnit
:
' On the worksheet, each of the name ranges, above, is examined and then two named ranges (axesmajor and axesminor) are then set to
'    the minimum values of the ranges above and then used to set the major and minor units of three charts so all three charts look similar
'    with the same vertical axes major and minor units.
:
    Sheet7.ChartObjects("startyear_rfb_chart").Chart.Axes(xlValue).MajorUnit = Range("axesmajor").Value
    Sheet7.ChartObjects("eoy_reserve_chart").Chart.Axes(xlValue).MajorUnit = Range("axesmajor").Value
    Sheet7.ChartObjects("startyear_reserve_expenses_chart").Chart.Axes(xlValue).MajorUnit = Range("axesmajor").Value
    Sheet7.ChartObjects("startyear_rfb_chart").Chart.Axes(xlValue).MinorUnit = Range("axesminor").Value
    Sheet7.ChartObjects("eoy_reserve_chart").Chart.Axes(xlValue).MinorUnit = Range("axesminor").Value
    Sheet7.ChartObjects("startyear_rfb_chart").Chart.Axes(xlValue).MinorUnit = Range("axesminor").Value
:
:
End Sub

Regards,

Steve
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,681
Members
449,048
Latest member
81jamesacct

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