Formatting axes based on cell values

doublej41

Board Regular
Joined
Mar 9, 2011
Messages
86
I am trying to set the maximum value for the x-axis of my chart based on the value I enter into a cell.

I am using the following VBA code under a worksheet change event:

Code:
Option Explicit
x1Category As Range


Private Sub Worksheet_Change(ByVal Target As Range)

Select Case Target.Address
    Case "$C$1"
        ActiveSheet.ChartObjects("Chart 1").Chart.Axes(x1Category) _
            .MaximumScale = Target.Value
    Case Else
End Select

End Sub

When I change the value in cell C1, I get a Run-time error 13 and the message 'Type mismatch'. Can anyone shed some light on what may be causing this error?

I am trying to do this with a XY scatter graph.

Thanks.
 
So, here is how the code on Sheet2 should look:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Me.Range("[COLOR="Blue"]D31[/COLOR]") Is Nothing Then

        If Sheets("[COLOR="blue"]Sheet1[/COLOR]").ChartObjects(1).Chart.Axes(xlCategory).MaximumScale <> Sheets("Sheet1").Range("[COLOR="blue"]AU10[/COLOR]").Value Then

            Sheets("[COLOR="blue"]Sheet1[/COLOR]").ChartObjects(1).Chart.Axes(xlCategory).MaximumScale = Sheets("Sheet1").Range("[COLOR="blue"]AU10[/COLOR]").Value

        End If

    End If

End Sub

Make sure the items in blue are correct. You've told me the two cell addresses, but not the name of the first sheet, which contains the formula and the chart.
 
Upvote 0

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.
OK the problem was I was using the sheet number from the VBA properties tables, as opposed to the name I have given the sheet.

Changing Sheet1 to the sheet name solves the issue. As far as I can tell the 2 sheets are now working as I need them to.

Thanks for all of your help, Jon. I appreciate your patience.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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