Home » excel » excel – VBA Chart.SetSourceData is switching my series and categories

excel – VBA Chart.SetSourceData is switching my series and categories

Posted by: admin April 23, 2020 Leave a comment


I’ve sunk about quite a bit of time into this and can’t seem to find a solution.

I’m using a worksheet selection change event that allows a person to modify the chart elements without having to open the chart formatting options. The issue is coming from when I attempt to use

.Chart.SetSourceData Union(rng0,rng5)

To explain a bit further.. my chart has 5 series and 4 columns of data:
rng0 contains the titles for series1-5,
rng1 contains the data for the first column of data for series 1-5,
rng2 contains the data for the second column of data for series 1-5,

I am allowing the user to decide which series of data (rng1-4) they would like the chart to display. The user makes their selections via a worksheet selection box that turns into a check once selected. I then test each option to determine which series of data to include in the chart. In all cases the data is non-contiguous as rng0 is a few columns over from rng1-4. I’ve tested whether this matters or not and it does not. I use a Union(rng5,rng1-4) to combine all of my selected ranges.

The union(rng0,rng5) does work, but it ends up swapping my axis. I have tried several things including:

myChart.PlotBy = xlRows  'This does not change the chart at all

I’ve also messing with the application.commandbars, but I couldn’t find the button to go to Chart Tools -> Design -> Switch rows/columns.

Below is a picture of the area and beneath that is the code. I appreciate your any help/ideas. If you need further info, let me know. Thanks


This is subset of the code as it is MUCH larger and contains some other things not relevant to this issue.

        Dim myChart As ChartObject
        Dim rng0 As Range
        Dim rng1 As Range
        Dim rng2 As Range
        Dim rng3 As Range
        Dim rng4 As Range
        Dim rng5 As Range

        'Slide area selected, set new subranges
        Set singleChart1 = Range("OTPSingleChart1Sel")
        'Test which chart is being modified
        'Single Chart1

        If Not Intersect(Target, singleChart1) Is Nothing Then
            Set myChart = otpWS.ChartObjects("OTPSingleChart1")
            With otpWS
                Set rng0 = Range("OTPSingleChart1Sel").Offset(0, 5)
                Set rng1 = Range("OTPSingleChart1Sel").Offset(0, 7)
                Set rng2 = Range("OTPSingleChart1Sel").Offset(0, 8)
                Set rng3 = Range("OTPSingleChart1Sel").Offset(0, 9)
                Set rng4 = Range("OTPSingleChart1Sel").Offset(0, 10)
            End With
            'Test offset
            If Target.Offset(0, 2).Value = "D2P" Then
                If Target.Value = "a" Then
                 'Test if at least one of our segments is on
                    With otpWS
                        If Application.WorksheetFunction.CountIf(.Range("OTPSingleChart1Sel").Range(.Cells(1, 1), .Cells(4, 1)), "a") > 1 Then
                            MsgBox ("At least one segment must be visible")
                            Exit Sub
                        End If
                    End With
                    Target.Value = ""
                    With otpWS
                        i = 1
                        For Each chk In .Range("OTPSingleChart1Sel").Range(.Cells(1, 1), .Cells(4, 1))
                            If chk.Value = "a" Then
                                If rng5 Is Nothing Then
                                    If i = 1 Then
                                        Set rng5 = rng1
                                    ElseIf i = 2 Then
                                        Set rng5 = rng2
                                    ElseIf i = 3 Then
                                        Set rng5 = rng3
                                    ElseIf i = 4 Then
                                        Set rng5 = rng4
                                    End If
                                    If i = 1 Then
                                        Set rng5 = Union(rng5, rng1)
                                    ElseIf i = 2 Then
                                        Set rng5 = Union(rng5, rng2)
                                    ElseIf i = 3 Then
                                        Set rng5 = Union(rng5, rng3)
                                    ElseIf i = 4 Then
                                        Set rng5 = Union(rng5, rng4)
                                    End If
                                End If
                            End If
                        i = i + 1
                        Next chk
                    End With
                    With myChart
                        .Chart.SetSourceData Union(rng0, rng5)
                    End With
How to&Answers:

Answer in comment

You mentioned you have used PlotBy, have you used it within SetSourceData? i.e. Chart.SetSourceData Union(rng0, rng5), xlRows


Answer supplied by Gary Evans in the comments to the OP.

Chart.SetSourceData Union(rng0,rng5), xlRows

seemed to do the trick.