Search code examples
vbagraphgridlines

Gridlines on Secondary axis


I have a graph (produced by a Userform in VBA) with both a primary and secondary y-axis exist. I wish to place gridlines only on the secondary axis, WITHOUT those on the Primary being present! How do I do this (??), at the moment gridlines appear for both the Primary and Secondary y-axis. This is the code I am using:

     With ActiveChart
       .HasTitle = True
       .ChartTitle.Characters.Text = "title"
       .Axes(xlCategory, xlPrimary).HasTitle = True
       .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Dates"
       .Axes(xlValue, xlPrimary).HasTitle = True
       .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Dollar"
       .Axes(xlValue, xlPrimary).TickLabels.NumberFormat = "0"
       .Axes(xlCategory).HasMajorGridlines = True
       .Axes(xlValue, xlSecondary).HasMinorGridlines = True
       .Legend.Position = xlLegendPositionRight

   End With

HELP!!!! Please, I would be most grateful for any assistance here! Thank you


Solution

  • If your Chart already has the Seconday Y-Axis configured, then you need to add the 2 following line inside your With statement:

    With ActiveChart
        ' do all your regular actions
    
        ' hide Primary Y-Axis Gridlines
        .SetElement (msoElementPrimaryValueGridLinesNone)
    
        ' Show Secondary Y-Axis Gridlines (Major)
        .SetElement (msoElementSecondaryValueGridLinesMajor)
    
    End With