方法 "SeriesCollection.Add" 未能通过正确的输入

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

我正在尝试使用 SeriesCollection.Add 方法通过代码在我的图表中添加一个新的 series/legend ,但它会抛出类型不匹配错误,如图所示。文档说 source 参数应该是范围类型,但是当给定一个范围时,它会在突出显示的行中抛出错误。同样的错误也发生在SetSourceData方法中,同样需要范围输入。 Documentation Link

代码:

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

发生错误的代码段:

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

 SelChart.SeriesCollection.Add Source:=xRange 

图片:

您的文档适用于 Excel.Chart,而您使用的是 PowerPoint.Chart

根据PowerPoint.Chart.SeriesCollection.Add method的文档,只需要传入字符串形式的地址即可:

SelChart.SeriesCollection.Add Source:=xRange.Address