I am trying to create a scatter chart using a macro that uses data in columns that are not adjacent. Once that is working, I would like to add more series to the chart, stepping across columns, but I can't get the first part to work. This is my code:
Sub Better_Chart()
   Dim chtChart As Chart
   Dim X As Integer
   Dim Y As Integer
   Dim Z As Integer
   'For X = Range("E1").Column To Range("K1").Column Step 6
    X = Range("E1").Column
    Z = Range("C1").Column
    Cells(14, X).Select
    Range(Selection, Selection.End(xlDown)).Select
    If Range("E14") <> Empty Then
    Y = Selection.Rows.Count + 13
    End If
 'Create a new chart.
 Set chtChart = Charts.Add
 Set chtChart = chtChart.Location(Where:=xlLocationAsObject, Name:="Yield Data")
 With chtChart
 '.Name = ""
 .ChartType = xlXYScatter
 'Link to the source data range.
' .SetSourceData Source:=Sheets("Yield Data").Range(Cells(14, Z), Cells(Y, X)),
 .SetSourceData Source:=Sheets("Yield Data").Union(Range(Cells(14, X), Cells(Y, X)), Range(Cells(14, Z), Cells(Y, Z))), _
 PlotBy:=xlColumns
' Next Y
 'Next X
 .HasTitle = True
 .ChartTitle.Text = Range("H3")
 .Axes(xlCategory, xlPrimary).HasTitle = True
 .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = Range("H5")
 .Axes(xlValue, xlPrimary).HasTitle = True
 .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = Range("H7")
 End With
End Sub
I get a debug error on the setSourceDate line. However, this code works perfectly, in that it selects the exact data I want to graph:
Public Sub Test_Union()
   Dim chtChart As Chart
   Dim X As Integer
   Dim Y As Integer
   Dim Z As Integer
   'For X = Range("E1").Column To Range("K1").Column Step 6
    X = Range("E1").Column
    Z = Range("C1").Column
    Cells(14, X).Select
    Range(Selection, Selection.End(xlDown)).Select
    If Range("E14") <> Empty Then
    Y = Selection.Rows.Count + 13
    End If
    'Range(Cells(14, Z), Cells(Y, X)).Select
     Union(Range(Cells(14, Z), Cells(Y, Z)), Range(Cells(14, X), Cells(Y, X))).Select
    'Range(Cells(14, Z), Cells(Y, X)).Select
' Next Y
 'Next X  
End Sub
Can anyone see what I am doing wrong?
 
     
    