Consulting

Results 1 to 4 of 4

Thread: Defining both axes titles for charts

  1. #1

    Defining both axes titles for charts

    i'm trying to set the axistitle object for my charts where the x axis is "m/s"
    and the y "hrs of day". i know how to set one as i've been using the following code:

    activechart.Axes(xlValue, xlPrimary).AxisTitle.Text = "m/s"
    however not sure how to set the other as when i've used 'record macro' to help figure out the code it doesn't give me the right code!! and just gives the following:

    activechart.Axes(xlvalue, xlPrimary).AxisTitle.Text = "hours of day"
    which has the effect of turning "m/s" into "hrs of day"

    how frustrating!

    regards
    mark
    Last edited by Aussiebear; 04-25-2023 at 07:09 PM. Reason: Added code tags

  2. #2
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    This will do the axis titles for 2d charts assuming there is a series on the secondary axis.


    Sub x()
    With ActiveChart
        With .Axes(xlValue, xlPrimary)
            .HasTitle = True
            .AxisTitle.Text = "Primary Y axis"
        End With
        With .Axes(xlCategory, xlPrimary)
           .HasTitle = True
            .AxisTitle.Text = "Primary X axis"
        End With
        With .Axes(xlValue, xlSecondary)
            .HasTitle = True
            .AxisTitle.Text = "Secondary Y axis"
        End With
        With .Axes(xlCategory, xlSecondary)
            .HasTitle = True
            .AxisTitle.Text = "Secondary X axis"
        End With
        End With
    End Sub
    Last edited by Aussiebear; 04-25-2023 at 07:10 PM. Reason: Adjusted the code tags
    Cheers
    Andy

  3. #3
    thanks andy

    that works great.

    just a small question, is it common practice in writing code to use lots of with/end with statements to cut down on code? i think i've started to notice this in others' code


    regards

    mark

  4. #4
    VBAX Regular
    Joined
    Oct 2017
    Posts
    20
    Location
    Excellent.
    Is very useful how to use this code lines to set axis names in excel chart axis from Ms Access.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •