jacobrcotton
Board Regular
- Joined
- Jan 28, 2017
- Messages
- 60
- Office Version
- 365
- Platform
- Windows
Hi Everyone,
I'm trying to set the X & Y Axes, Primary & Secondary mins and maxs (and other general chart properties) through a UDF, which i can then use as a simple formula within my workbook. I've run across some code and i'm trying to break it down using simple data, then build it back up so know exactly what its doing and why its breaking.
The bulk of my original code was jacked via Excel Off The Grid (credit). Code below, my questions are:
(1) This code works great for my Y-axis, but it doesn't work at all for my X-axis (the X-axis is also numeric, between 1 - 10). I haven't a clue why not.
(2) For some reason, when i enter my function into a worksheet cell, i get a #Value! error when i use an XorYAxis input of "X". But if i use "x", the function completes, but like in (1) above, the X-Axis isn't working.
My data is a simple table of X = 1->10, Y = X*10. So line 1 is X=1, Y = 10; X=2, Y=20,...,X=10, Y=100.
My slimmed down code:
(2i)
Returns: #Value!
(2ii)
Returns last line of code: "x Prim Max: 40"
I'm trying to set the X & Y Axes, Primary & Secondary mins and maxs (and other general chart properties) through a UDF, which i can then use as a simple formula within my workbook. I've run across some code and i'm trying to break it down using simple data, then build it back up so know exactly what its doing and why its breaking.
The bulk of my original code was jacked via Excel Off The Grid (credit). Code below, my questions are:
(1) This code works great for my Y-axis, but it doesn't work at all for my X-axis (the X-axis is also numeric, between 1 - 10). I haven't a clue why not.
(2) For some reason, when i enter my function into a worksheet cell, i get a #Value! error when i use an XorYAxis input of "X". But if i use "x", the function completes, but like in (1) above, the X-Axis isn't working.
My data is a simple table of X = 1->10, Y = X*10. So line 1 is X=1, Y = 10; X=2, Y=20,...,X=10, Y=100.
My slimmed down code:
VBA Code:
Option Explicit
Function setChartAxis(sheetName As String, _
chartName As String, _
XorYAxis As String, _
PrimOrSec As String, _
MinOrMax As String, _
Value As Double)
'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 X-axis Min&Max
If XorYAxis = "X" And PrimOrSec = "Prim" Then
With cht.Axes(xlCategory, xlPrimary)
If MinOrMax = "Min" Then .MinimumScale = Value
If MinOrMax = "Max" Then .MaximumScale = Value
End With
End If
'Set Y-axis Min&Max
If XorYAxis = "Y" And PrimOrSec = "Prim" Then
With cht.Axes(xlValue, xlPrimary)
If MinOrMax = "Min" Then .MinimumScale = Value
If MinOrMax = "Max" Then .MaximumScale = Value
End With
End If
setChartAxis = XorYAxis & " " & PrimOrSec & " " & MinOrMax & ": " & Value
End Function
(2i)
Excel Formula:
=setChartAxis("Sheet1","Chart 1","X","Prim","Max",40)
(2ii)
Excel Formula:
=setChartAxis("Sheet1","Chart 1","x","Prim","Max",40)