Charlie
Charlie

Reputation: 175

Method "SeriesCollection.Add" failing for a correct input passed

I am trying to add a new series/legend in my chart via code using the SeriesCollection.Add method, but it throws in Type mismatch error as shown. Documentation says the source argument should be of range type but when given a range, it throws in the error at highlighted line. The same error happens in the SetSourceData method too, which also needs a range input. Documentation Link

CODE:

If SetDelShape.HasChart Then
                               
Dim SelChart As PowerPoint.Chart
Set SelChart = SetDelShape.Chart
Dim Selchtdat As PowerPoint.ChartData
Set Selchtdat = SelChart.ChartData
Dim OriSheet As Worksheet
Set OriSheet = Selchtdat.Workbook.Sheets(1)
Dim SelSheet As Worksheet
Set SelSheet = Selchtdat.Workbook.Sheets("TempSheet")
Dim SelSheetlrow As Long
SelSheetlrow = SelSheet.Cells(SelSheet.Rows.Count, "A").End(xlUp).Row
Dim SelSheetlcol As Long
SelSheetlcol = SelSheet.Cells(1, SelSheet.Columns.Count).End(xlToLeft).Column
Dim OriSheetlrow As Long
OriSheetlrow = OriSheet.Cells(OriSheet.Rows.Count, "A").End(xlUp).Row

Dim SwapRnge1 As Range
Dim TempRange1 As Variant
Set SwapRnge1 = OriSheet.Range("A1:A" & OriSheetlrow)
TempRange1 = SwapRnge1
                                    
Dim SwapRnge2 As Range
Dim TempRange2 As Variant
Set SwapRnge2 = SelSheet.Range("A1:" & NumToLet(SelSheet, SelSheetlcol) & SelSheetlrow)
TempRange2 = SwapRnge2
   
On Error Resume Next
Selchtdat.Activate
On Error GoTo 0
Excel.Application.Visible = True

' Here new line items are populated in a dictionary. Code not included because a bit complex

Dim oIndex, jIndex As Integer
Dim lrowbrand As Long

 For Each Key In dict.keys
    If dict(Key) = "NotPresent" And dict(Key) <> "" Then
       lrowbrand = OriSheet.Cells(OriSheet.Rows.Count, "A").End(xlUp).Row
             For oIndex = 1 To UBound(TempRange2, 1)
                    If LCase(SwapRnge2.Cells(oIndex, 1).Value) = Key Then   
                         SelSheet.Range("A" & oIndex & ":" & NumToLet(SelSheet, SelSheetlcol) & oIndex).Copy
                         OriSheet.Range("A" & (lrowbrand + 1) & ":" & NumToLet(SelSheet, SelSheetlcol) & lrowbrand + 1).PasteSpecial (xlPasteValuesAndNumberFormats) 
                         Dim xRange As Range
                         Set xRange = OriSheet.Range(OriSheet.Cells((lrowbrand + 1), 1), OriSheet.Cells((lrowbrand + 1), SelSheetlcol))
                         SelChart.SeriesCollection.Add Source:=xRange ', Rowcol:=xlRows
                         Exit For
                     End If
               Next
       End If
 Next

End if

Snippet where error occurs:

 Dim xRange As Range
 Set xRange = OriSheet.Range(OriSheet.Cells((lrowbrand + 1), 1), _
                             OriSheet.Cells((lrowbrand + 1), SelSheetlcol))

 SelChart.SeriesCollection.Add Source:=xRange 

PICS:

enter image description here

enter image description here

enter image description here

Upvotes: 0

Views: 167

Answers (1)

Raymond Wu
Raymond Wu

Reputation: 3387

Your documentation is for Excel.Chart whereas you are working with PowerPoint.Chart.

Based on the documentation of PowerPoint.Chart.SeriesCollection.Add method, you only need to pass the address in string so:

SelChart.SeriesCollection.Add Source:=xRange.Address

Upvotes: 1

Related Questions