Charts determine interval for Major Grid lines

What is the calculation used when charts determined the interval for the y axix major grid lines? I'm creating SSRS report which includes a chart. I want more Major grid lines than what is displayed by the auto generated.  So I want to write an expression in the Major grid line interval. I can't use a set value because my data ranges will change. Example: Base on user selection values can range from -3000 to 4000 or 0 to 600. Wont know til run time. But with those two examples, I should have interval of 1000 for the first example and 100 for the other. What the auto generated lines are at 2000 for the first example.
So If I knew the calculation used, I figure I could write an expression.  Thanks in advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In tha attached file is the macro shown below. It's an idea of the sort of thing that can be done. Adapt it to meet your needs and then Alt+F8 to run the macro.
Hope it helps

Sub chart_scales()
Dim minx As Double
Dim maxx As Double
Dim maju As Double
minx = WorksheetFunction.Min(Range(Sheets("Sheet1").[B1], Sheets("Sheet1").[B13]))
maxx = WorksheetFunction.Max(Range(Sheets("Sheet1").[B1], Sheets("Sheet1").[B13]))
Select Case maxx
    Case Is <= 1000 'change to suit your needs
        maju = 100 'change to suit your needs
    Case Is >= 1000 'change to suit your needs
        maju = 2000 'change to suit your needs
End Select
ActiveSheet.ChartObjects("Chart 1").Activate
With ActiveChart.Axes(xlValue)
    .MinimumScale = minx - maju
    .MaximumScale = maxx + maju
    .MinorUnitIsAuto = True
    .MajorUnit = maju
    .Crosses = xlAutomatic
    .ReversePlotOrder = False
    .ScaleType = xlLinear
    .DisplayUnit = xlNone
End With
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
byundtMechanical EngineerCommented:
After thinking about Patrick's approach, I thought you might want to generalize it by expressing the range (max value - min value) in scientific notation. In so doing, you get a radix (a number between 1 and 9.999) times a power of 10. By using the radix in Patrick's Select Case block, you make the logic applicable to any arbitrary range of numbers.

The macro in the snippet below, was based on Patrick's code. It applies to a chart that you select before running the macro. It determines the min and max values from the actual data used by that chart. It then determines the major unit scale that is most appropriate for your data. Finally, it sets the minimum and maximum values on the Value axis according to the major unit, going above and below your data by at most one major unit.

As Patrick suggested, you can add as many cases as you like to the Select Case block of code. As written, you will get between 6 and 12 major grid lines running through your actual data.

Sub chart_scales2()
Dim minx As Double, maxx As Double, maju As Double
Dim pwr10 As Double, radix As Double, d As Double
Dim sRange As String
Dim cht As Chart
Dim ser1 As Series
Set cht = ActiveChart
Set ser1 = cht.SeriesCollection(1)
minx = Application.Min(ser1.Values)
maxx = Application.Max(ser1.Values)
sRange = Format(maxx - minx, "0.00E+00")
radix = Val(Left(sRange, InStr(1, sRange, "E") - 1))
pwr10 = 10 ^ Val(Mid(sRange, 6))
Select Case radix          'Code stops at the first match
Case Is <= 1.2
    maju = 0.1 * pwr10
Case Is <= 1.8
    maju = 0.2 * pwr10
Case Is <= 2.5
    maju = 0.25 * pwr10
Case Is <= 3.5
    maju = 0.4 * pwr10
Case Is <= 6
    maju = 0.5 * pwr10
Case Else
    maju = 1 * pwr10
End Select
With cht.Axes(xlValue)
    .MinimumScale = Int(minx / maju) * maju
    .MaximumScale = (Int(maxx / maju) + 1) * maju
    .MinorUnitIsAuto = True
    .MajorUnit = maju
    .Crosses = xlAutomatic
    .ReversePlotOrder = False
    .ScaleType = xlLinear
    .DisplayUnit = xlNone
End With
End Sub

Open in new window

MadIceAuthor Commented:
Thanks for the replys. I'm using BIDs to create these reports which I'm new at using and just found where I can use code on my reports. I thought I could only use an expression. I need to read up on using the code tab and then I will try the suggestions. It will be a few days...Thanks
MadIceAuthor Commented:
I think I can use both of your suggestions to come up with what I'm looking for
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.