Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 62
  • Last Modified:

Switch the axis of series in a Excel chart

Hello,

I have written the following code to generate a Excel chart from Access.  It generates a chart with a Primary and Secondary Axis.  It works fine and generated the chart.  However, the client wants me to reverse the data set that populate each axis so that the data formerly popluating the primary axis will now populate the secondary axis and the data corresponding to the secondary axis will now populate the Primary axis.  When I set the axisgroup to 2 for the ex primary series and 1 for the former secondary axis, the primary axis would not populate.  I then inadvertently set the  axis group to 2 for all the series and lo and behold, everything worked fine.  I just don't understand why.  What am I missing?
Original Code
Private Sub Create_NEX_LIFR_Weekly_YDT_Chart(objWS As Object)
    
    Dim objXlSheet As Object
    Dim objXlChart As Object
    Dim objXlDataSheet As Object
    Dim objRng As Object
    Dim objRngChart1 As Object
    Dim objRngChart2 As Object
    Dim objSeries As Object
    Dim lngY As Long
    Dim NewSeries As Object

    Set objXlSheet = objWS
    Dim objTextbox As Object
    
    Set objXlDataSheet = mobjXlBook.Worksheets("NEX_LIFR_Weekly_YTD")
    Set objRng = objXlDataSheet.Range("A:A,G:G")
    lngY = objXlDataSheet.usedRange.rows.Count
    lngY = lngY + 10
    
    Set objXlChart = objXlSheet.chartobjects.Add(Left:=75, Width:=750, Top:=lngY, Height:=600).Chart
    
    
    Set objTextbox = objXlChart.Shapes.AddTextbox(1, 1, 1, 100, 20)
    objTextbox.select
    With objTextbox.TextEffect
        .Text = Format(Date, "mmm-dd-yyyy")
        .FontBold = True
    End With
    
    With objXlChart
        .SetSourceData Source:=objXlDataSheet.Range("A:B")
        
        .ChartType = 51  '2D clustered columns
        
        .HasTitle = True
        .ChartTitle.Text = "NEX LIFR Weekly YTD " & Year(Date)
        .HasLegend = True
        .PlotBy = 2
        .Axes(1).MajorUnit = 7

        .SetSourceData Source:=objXlDataSheet.Range("A:C")
        .SetSourceData Source:=objXlDataSheet.Range("A:D")
        .SetSourceData Source:=objXlDataSheet.Range("A:E")
        
        Set NewSeries = objXlChart.SeriesCollection(1)
        With NewSeries
            .Name = "# Lines (Non-NEX)"
            .AxisGroup = 1
        End With
        
        Set NewSeries = objXlChart.SeriesCollection(2)
        With NewSeries
            .Name = "# Lines Filled (Non-NEX)"
            .AxisGroup = 1
        End With
        
        Set NewSeries = objXlChart.SeriesCollection(3)
        With NewSeries
            .Name = "# Lines (NEX)"
            .AxisGroup = 1
        End With
        
        Set NewSeries = objXlChart.SeriesCollection(4)
        With NewSeries
            .Name = "# Lines Filled (NEX)"
            .AxisGroup = 1
        End With
        
        Set NewSeries = .SeriesCollection.NewSeries
        With NewSeries
            .Name = "Aggregate LIFR (Non-NEX)"
            .Values = objXlDataSheet.Range("F:F")
            .AxisGroup = 2
            .ChartType = 65
            .Format.Line.Weight = 1
        End With
        
        Set NewSeries = .SeriesCollection.NewSeries
        With NewSeries
            .Name = "Aggregate LIFR (NEX)"
            .Values = objXlDataSheet.Range("G:G")
            .AxisGroup = 2
            .ChartType = 65
            .Format.Line.Weight = 1
        End With

    End With
    
    With objXlChart.ChartArea.Format.Fill
        .Visible = True
        .ForeColor.SchemeColor = 41
        .BackColor.SchemeColor = 41
        .TwoColorGradient Style:=1, Variant:=1
    End With
    
    With objXlChart.PlotArea.Format.Fill
        .Visible = True
        .ForeColor.SchemeColor = 41
        .BackColor.SchemeColor = 41
        .TwoColorGradient Style:=1, Variant:=2
    End With


    
End Sub

Open in new window


Working Code
Private Sub Create_NEX_LIFR_Weekly_YDT_Chart(objWS As Object)
    
    Dim objXlSheet As Object
    Dim objXlChart As Object
    Dim objXlDataSheet As Object
    Dim objRng As Object
    Dim objRngChart1 As Object
    Dim objRngChart2 As Object
    Dim objSeries As Object
    Dim lngY As Long
    Dim NewSeries As Object

    Set objXlSheet = objWS
    Dim objTextbox As Object
    
    Set objXlDataSheet = mobjXlBook.Worksheets("NEX_LIFR_Weekly_YTD")
    Set objRng = objXlDataSheet.Range("A:A,G:G")
    lngY = objXlDataSheet.usedRange.rows.Count
    lngY = lngY + 10
    
    Set objXlChart = objXlSheet.chartobjects.Add(Left:=75, Width:=750, Top:=lngY, Height:=600).Chart
    
    
    Set objTextbox = objXlChart.Shapes.AddTextbox(1, 1, 1, 100, 20)
    objTextbox.select
    With objTextbox.TextEffect
        .Text = Format(Date, "mmm-dd-yyyy")
        .FontBold = True
    End With
    
    With objXlChart
        .SetSourceData Source:=objXlDataSheet.Range("A:B")
        
        .ChartType = 51  '2D clustered columns
        
        .HasTitle = True
        .ChartTitle.Text = "NEX LIFR Weekly YTD " & Year(Date)
        .HasLegend = True
        .PlotBy = 2
        .Axes(1).MajorUnit = 7

        .SetSourceData Source:=objXlDataSheet.Range("A:C")
        .SetSourceData Source:=objXlDataSheet.Range("A:D")
        .SetSourceData Source:=objXlDataSheet.Range("A:E")
        
        Set NewSeries = objXlChart.SeriesCollection(1)
        With NewSeries
            .Name = "# Lines (Non-NEX)"
            .AxisGroup = 2
        End With
        
        Set NewSeries = objXlChart.SeriesCollection(2)
        With NewSeries
            .Name = "# Lines Filled (Non-NEX)"
            .AxisGroup = 2
        End With
        
        Set NewSeries = objXlChart.SeriesCollection(3)
        With NewSeries
            .Name = "# Lines (NEX)"
            .AxisGroup = 2
        End With
        
        Set NewSeries = objXlChart.SeriesCollection(4)
        With NewSeries
            .Name = "# Lines Filled (NEX)"
            .AxisGroup = 2
        End With
        
        Set NewSeries = .SeriesCollection.NewSeries
        With NewSeries
            
            .Name = "Aggregate LIFR (Non-NEX)"
            .Values = objXlDataSheet.Range("F:F")
            .AxisGroup = 2
            .ChartType = 65
            .Format.Line.Weight = 1
        End With
        
        Set NewSeries = .SeriesCollection.NewSeries
        With NewSeries
            .Name = "Aggregate LIFR (NEX)"
            .Values = objXlDataSheet.Range("G:G")
            .AxisGroup = 2
            .ChartType = 65
            .Format.Line.Weight = 1
        End With

    End With
    
    With objXlChart.ChartArea.Format.Fill
        .Visible = True
        .ForeColor.SchemeColor = 41
        .BackColor.SchemeColor = 41
        .TwoColorGradient Style:=1, Variant:=1
    End With
    
    With objXlChart.PlotArea.Format.Fill
        .Visible = True
        .ForeColor.SchemeColor = 41
        .BackColor.SchemeColor = 41
        .TwoColorGradient Style:=1, Variant:=2
    End With


    
End Sub

Open in new window

0
chtullu135
Asked:
chtullu135
1 Solution
 
aikimarkCommented:
If you were to have used the chart wizard to create these, you would see two radio buttons that allow you to set by rows or by columns for the plotting/graphing.  

Hypothesis: Since it appears early in the dialog sequence, I assume that its order is critical to the later decisions and actions.  In the unsuccessful case, your code was changing this value.  The changed value (downstream) was what caused the behavior you observed.

Test: Only have one (initial) setting of the primary axis in your code.  Comment/remove the others.  What you should see is the expected behavior of setting the primary axis to by-row or by-column.
0
 
chtullu135Author Commented:
I just noticed that in the code that I thought was working, that I lost my X axis.  I did not see the date values displayed on the x axis
0
 
chtullu135Author Commented:
I retested everything and found that when I changed the code to the following, that everything populated correctly.  (I've attached a pdf showing the chart.  However, when I try to reverse the vertical axis via the axis group property so that the one on the left appears on the right and the right axis is displayed on the left, the left vertical axis is blank as is the horizontal axis.  I attached two pdf files of the charts for both cases
Private Sub Create_NEX_LIFR_Weekly_YDT_Chart(objWS As Object)
    
    Dim objXlSheet As Object
    Dim objXlChart As Object
    Dim objXlDataSheet As Object
    Dim objRng As Object
    Dim objRngChart1 As Object
    Dim objRngChart2 As Object
    Dim objSeries As Object
    Dim lngY As Long
    Dim NewSeries As Object

    Set objXlSheet = objWS
    Dim objTextbox As Object
    
    Set objXlDataSheet = mobjXlBook.Worksheets("NEX_LIFR_Weekly_YTD")
    Set objRng = objXlDataSheet.Columns("A:A").Find("*", objXlDataSheet.[a1], -4163, , 1, 2)

    lngY = objXlDataSheet.usedRange.rows.Count
    lngY = lngY + 10
    
    Set objXlChart = objXlSheet.ChartObjects.Add(Left:=75, Width:=750, Top:=lngY, Height:=600).Chart
    
    Set objTextbox = objXlChart.Shapes.AddTextbox(1, 1, 1, 100, 20)
    objTextbox.select
    With objTextbox.TextEffect
        .Text = Format(Date, "mmm-dd-yyyy")
        .FontBold = True
    End With
    
    Set objTextbox = objXlChart.Shapes.AddTextbox(1, 600, 1, 150, 20)
    objTextbox.select
    With objTextbox.TextEffect
        .Text = "WE " & Format(objRng.Value, "mmm-dd-yyyy")
        .FontSize = 14
        .FontBold = True
    End With
    
    With objXlChart
        .SetSourceData Source:=objXlDataSheet.Range("A:B")
        
        .ChartType = 51  '2D clustered columns
        
        .HasTitle = True
        .ChartTitle.Text = "NEX LIFR Weekly YTD " & Year(Date)
        .HasLegend = True
        .Legend.Position = -4107
        .PlotBy = 2
        .Axes(1).MajorUnit = 7

        .SetSourceData Source:=objXlDataSheet.Range("A:C")
        .SetSourceData Source:=objXlDataSheet.Range("A:D")
        .SetSourceData Source:=objXlDataSheet.Range("A:E")
        
        Set NewSeries = objXlChart.SeriesCollection(1)
        With NewSeries
            .Name = "# Lines (Non-NEX)"
            .AxisGroup = 1
        End With
        
        Set NewSeries = objXlChart.SeriesCollection(2)
        With NewSeries
            .Name = "# Lines Filled (Non-NEX)"
            .AxisGroup = 1
        End With
        
        Set NewSeries = objXlChart.SeriesCollection(3)
        With NewSeries
            .Name = "# Lines (NEX)"
            .AxisGroup = 1
        End With
        
        Set NewSeries = objXlChart.SeriesCollection(4)
        With NewSeries
            .Name = "# Lines Filled (NEX)"
            .AxisGroup = 1
        End With
        
        Set NewSeries = .SeriesCollection.NewSeries
        With NewSeries
            
            .Name = "Aggregate LIFR (Non-NEX)"
            .Values = objXlDataSheet.Range("F:F")
            .AxisGroup = 2
            .ChartType = 65
            .Format.Line.Weight = 1
        End With
        
        Set NewSeries = .SeriesCollection.NewSeries
        With NewSeries
            .Name = "Aggregate LIFR (NEX)"
            .Values = objXlDataSheet.Range("G:G")
            .AxisGroup = 2
            .ChartType = 65
            .Format.Line.Weight = 1
        End With

    End With
    
    With objXlChart.ChartArea.Format.Fill
        .Visible = True
        .ForeColor.SchemeColor = 41
        .BackColor.SchemeColor = 41
        .TwoColorGradient Style:=1, Variant:=1
    End With
    
    With objXlChart.PlotArea.Format.Fill
        .Visible = True
        .ForeColor.SchemeColor = 41
        .BackColor.SchemeColor = 41
        .TwoColorGradient Style:=1, Variant:=2
    End With

Call SaveToPdf(objXlSheet)
    
End Sub

Open in new window

Sheet1.pdf
Sheet2.pdf
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Martin LissRetired ProgrammerCommented:
I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
0
 
chtullu135Author Commented:
I played around with the code and the following code gives me the results I wanted.

Private Sub Create_LIFR_Weekly_YDT_Chart(objWS As Object)
    ' Comments:
    ' Params  : objWS
    ' Modified:
    
    On Error GoTo PROC_ERR
    
    Dim objXlSheet As Object
    Dim objXlChart As Object
    Dim objXlDataSheet As Object
    Dim objRng As Object
    Dim objRngChart1 As Object
    Dim objRngChart2 As Object
    Dim objSeries As Object
    Dim lngY As Long
    Dim objNewSeries As Object
    
    Set objXlSheet = objWS
    Dim objTextbox As Object
    
    Set objXlDataSheet = mobjXlBook.Worksheets("LIFR_Weekly_YTD")
    Set objRng = objXlDataSheet.Columns("A:A").Find("*", objXlDataSheet.[a1], -4163, , 1, 2)
    
    lngY = objXlDataSheet.usedRange.Rows.Count
    lngY = lngY + 10
    
    Set objXlChart = objXlSheet.ChartObjects.Add(Left:=75, Width:=750, Top:=lngY, Height:=600).Chart
    
    Set objTextbox = objXlChart.Shapes.AddTextbox(1, 1, 1, 100, 20)
    objTextbox.Select
    With objTextbox.TextEffect
        .Text = Format(Date, "mmm-dd-yyyy")
        .FontBold = True
    End With
    
    Set objTextbox = objXlChart.Shapes.AddTextbox(1, 600, 1, 150, 20)
    objTextbox.Select
    With objTextbox.TextEffect
        .Text = "WE " & Format(objRng.Value, "mmm-dd-yyyy")
        .FontSize = 14
        .FontBold = True
    End With
    
    With objXlChart
        .SetSourceData Source:=objXlDataSheet.Range("A:B")
        
        .ChartType = 51  '2D clustered columns
        
        .HasTitle = True
        .ChartTitle.Text = "LIFR Weekly YTD " & Year(Date)
        .HasLegend = True
        .Legend.Position = -4107
        .PlotBy = 2
        .Axes(1).MajorUnit = 7
        .Axes(1).ticklabels.Orientation = 60
        .Axes(1).ticklabels.NumberFormat = "mmm-dd"
        .SetSourceData Source:=objXlDataSheet.Range("A:C")
        .SetSourceData Source:=objXlDataSheet.Range("A:D")
        .SetSourceData Source:=objXlDataSheet.Range("A:E")
        
        Set objNewSeries = objXlChart.SeriesCollection(1)
        With objNewSeries
            .Name = "Total NEX Lines"
            .AxisGroup = 1
            '.ApplyDataLabels
            '.DataLabels.NumberFormat = "0"
        End With
        
        Set objNewSeries = objXlChart.SeriesCollection(2)
        With objNewSeries
            .Name = "NEX Lines Avail"
            .AxisGroup = 1
            '.ApplyDataLabels
            '.DataLabels.NumberFormat = "0"
        End With
        
        Set objNewSeries = objXlChart.SeriesCollection(3)
        With objNewSeries
            .Name = "Total Non-NEX Lines"
            .AxisGroup = 1
            '.ApplyDataLabels
            '.DataLabels.NumberFormat = "0"
        End With
        
        Set objNewSeries = objXlChart.SeriesCollection(4)
        With objNewSeries
            .Name = "Non-NEX Lines Avail"
            .AxisGroup = 1
            '.ApplyDataLabels
            '.DataLabels.NumberFormat = "0"
        End With
        
        Set objNewSeries = .SeriesCollection.NewSeries
        With objNewSeries
            .Name = "% NEX Lines Avail"
            '.Values = objXlDataSheet.Range("F:F")
            .Values = objXlDataSheet.Range(objXlDataSheet.Range("F2"), objXlDataSheet.Range("F" & objXlDataSheet.cells.Rows.Count).End(-4162))
            .AxisGroup = 2
            .ChartType = 65
            .Format.Line.Weight = 1
            .ApplyDataLabels
            .DataLabels.NumberFormat = "0.0%"
            .DataLabels.Position = 0
        End With
        
        Set objNewSeries = .SeriesCollection.NewSeries
        With objNewSeries
            .Name = "% Non-NEX Lines Avail"
            '.Values = objXlDataSheet.Range("G:G")
            .Values = objXlDataSheet.Range(objXlDataSheet.Range("G2"), objXlDataSheet.Range("G" & objXlDataSheet.cells.Rows.Count).End(-4162))
            .AxisGroup = 2
            .ChartType = 65
            .Format.Line.Weight = 1
            .ApplyDataLabels
            .DataLabels.NumberFormat = "0.0%"
            .DataLabels.Position = 1
        End With
        
    End With
    
    With objXlChart.Axes(2, 2)
        .MinimumScale = 0
        .MaximumScale = 1
    End With
    
    With objXlChart.ChartArea.Format.Fill
        .Visible = True
        .ForeColor.SchemeColor = 41
        .BackColor.SchemeColor = 41
        .TwoColorGradient Style:=1, Variant:=1
    End With
    
    With objXlChart.PlotArea.Format.Fill
        .Visible = True
        .ForeColor.SchemeColor = 41
        .BackColor.SchemeColor = 41
        .TwoColorGradient Style:=1, Variant:=2
    End With
    
    Call SaveToPdf(objXlSheet)
    
PROC_EXIT:
    Exit Sub
    
PROC_ERR:
    MsgBox Err.Number & vbTab & Err.Description, vbCritical, Me.Name & ".Create_NEX_LIFR_Weekly_YDT_Chart"
    Resume PROC_EXIT

Open in new window

0
 
chtullu135Author Commented:
I was able to get the code working correctly

Private Sub Create_LIFR_Weekly_YDT_Chart(objWS As Object)
    ' Comments:
    ' Params  : objWS
    ' Modified:
    
    On Error GoTo PROC_ERR
    
    Dim objXlSheet As Object
    Dim objXlChart As Object
    Dim objXlDataSheet As Object
    Dim objRng As Object
    Dim objRngChart1 As Object
    Dim objRngChart2 As Object
    Dim objSeries As Object
    Dim lngY As Long
    Dim objNewSeries As Object
    
    Set objXlSheet = objWS
    Dim objTextbox As Object
    
    Set objXlDataSheet = mobjXlBook.Worksheets("LIFR_Weekly_YTD")
    Set objRng = objXlDataSheet.Columns("A:A").Find("*", objXlDataSheet.[a1], -4163, , 1, 2)
    
    lngY = objXlDataSheet.usedRange.Rows.Count
    lngY = lngY + 10
    
    Set objXlChart = objXlSheet.ChartObjects.Add(Left:=75, Width:=750, Top:=lngY, Height:=600).Chart
    
    Set objTextbox = objXlChart.Shapes.AddTextbox(1, 1, 1, 100, 20)
    objTextbox.Select
    With objTextbox.TextEffect
        .Text = Format(Date, "mmm-dd-yyyy")
        .FontBold = True
    End With
    
    Set objTextbox = objXlChart.Shapes.AddTextbox(1, 600, 1, 150, 20)
    objTextbox.Select
    With objTextbox.TextEffect
        .Text = "WE " & Format(objRng.Value, "mmm-dd-yyyy")
        .FontSize = 14
        .FontBold = True
    End With
    
    With objXlChart
        .SetSourceData Source:=objXlDataSheet.Range("A:B")
        
        .ChartType = 51  '2D clustered columns
        
        .HasTitle = True
        .ChartTitle.Text = "LIFR Weekly YTD " & Year(Date)
        .HasLegend = True
        .Legend.Position = -4107
        .PlotBy = 2
        .Axes(1).MajorUnit = 7
        .Axes(1).ticklabels.Orientation = 60
        .Axes(1).ticklabels.NumberFormat = "mmm-dd"
        .SetSourceData Source:=objXlDataSheet.Range("A:C")
        .SetSourceData Source:=objXlDataSheet.Range("A:D")
        .SetSourceData Source:=objXlDataSheet.Range("A:E")
        
        Set objNewSeries = objXlChart.SeriesCollection(1)
        With objNewSeries
            .Name = "Total NEX Lines"
            .AxisGroup = 1
            '.ApplyDataLabels
            '.DataLabels.NumberFormat = "0"
        End With
        
        Set objNewSeries = objXlChart.SeriesCollection(2)
        With objNewSeries
            .Name = "NEX Lines Avail"
            .AxisGroup = 1
            '.ApplyDataLabels
            '.DataLabels.NumberFormat = "0"
        End With
        
        Set objNewSeries = objXlChart.SeriesCollection(3)
        With objNewSeries
            .Name = "Total Non-NEX Lines"
            .AxisGroup = 1
            '.ApplyDataLabels
            '.DataLabels.NumberFormat = "0"
        End With
        
        Set objNewSeries = objXlChart.SeriesCollection(4)
        With objNewSeries
            .Name = "Non-NEX Lines Avail"
            .AxisGroup = 1
            '.ApplyDataLabels
            '.DataLabels.NumberFormat = "0"
        End With
        
        Set objNewSeries = .SeriesCollection.NewSeries
        With objNewSeries
            .Name = "% NEX Lines Avail"
            '.Values = objXlDataSheet.Range("F:F")
            .Values = objXlDataSheet.Range(objXlDataSheet.Range("F2"), objXlDataSheet.Range("F" & objXlDataSheet.cells.Rows.Count).End(-4162))
            .AxisGroup = 2
            .ChartType = 65
            .Format.Line.Weight = 1
            .ApplyDataLabels
            .DataLabels.NumberFormat = "0.0%"
            .DataLabels.Position = 0
        End With
        
        Set objNewSeries = .SeriesCollection.NewSeries
        With objNewSeries
            .Name = "% Non-NEX Lines Avail"
            '.Values = objXlDataSheet.Range("G:G")
            .Values = objXlDataSheet.Range(objXlDataSheet.Range("G2"), objXlDataSheet.Range("G" & objXlDataSheet.cells.Rows.Count).End(-4162))
            .AxisGroup = 2
            .ChartType = 65
            .Format.Line.Weight = 1
            .ApplyDataLabels
            .DataLabels.NumberFormat = "0.0%"
            .DataLabels.Position = 1
        End With
        
    End With
    
    With objXlChart.Axes(2, 2)
        .MinimumScale = 0
        .MaximumScale = 1
    End With
    
    With objXlChart.ChartArea.Format.Fill
        .Visible = True
        .ForeColor.SchemeColor = 41
        .BackColor.SchemeColor = 41
        .TwoColorGradient Style:=1, Variant:=1
    End With
    
    With objXlChart.PlotArea.Format.Fill
        .Visible = True
        .ForeColor.SchemeColor = 41
        .BackColor.SchemeColor = 41
        .TwoColorGradient Style:=1, Variant:=2
    End With
    
    Call SaveToPdf(objXlSheet)
    
PROC_EXIT:
    Exit Sub
    
PROC_ERR:
    MsgBox Err.Number & vbTab & Err.Description, vbCritical, Me.Name & ".Create_NEX_LIFR_Weekly_YDT_Chart"
    Resume PROC_EXIT

Open in new window

0
 
WhackAModCommented:
Closed, 500 points refunded.
WhackAMod
Community Support Moderator
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now