我正在使用VBA更新几个图表的系列。我有一些图表显示3系列。我可以更新这些,但是当我尝试更新x-axis
时,我会收到错误。这是我到目前为止所得到的:
ActiveChart.SeriesCollection(1).XValues = "=" & Sheets(1).Range("P5", Sheets(1).Cells(5, Sheets(1).Columns.Count).End(xlToLeft).Address).Address(True, True, xlA1, xlExternal)
我已经尝试过测试这个任务的每个部分,每个部分的工作都应该如此。
我激活了正确的图表,并为作业选择了正确的值。这是我第一次通过VBA制作图表,所以我确信我错过了一些非常明显的东西。
我有一些额外的代码:
If Counter = 4 Then
Dim XAxisRng
ActiveSheet.ChartObjects(Chart).Activate
With Sheets(1)
Set XAxisRng = .Range("P5", .Cells(5, .Columns.Count).End(xlToLeft))
End With
ActiveChart.SeriesCollection(1).XValues = "=" & XAxisRng.Address(True, True, xlA1, xlExternal ActiveChart.SeriesCollection(1).Values = "=" & Sheets(1).Range("P6:" & GetLetterFromNumber(Sheets(1).Cells(6, Sheets(1).Columns.Count).End(xlToLeft).Column) & "6").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(2).Values = "=" & Sheets(2).Range("P6:" & GetLetterFromNumber(Sheets(2).Cells(6, Sheets(2).Columns.Count).End(xlToLeft).Column) & "6").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(3).Values = "=" & Sheets(3).Range("P6:" & GetLetterFromNumber(Sheets(3).Cells(6, Sheets(3).Columns.Count).End(xlToLeft).Column) & "6").Address(True, True, xlAl, xlExternal)
更新
我已将代码更新为以下内容:
If Counter = 4 Then
Dim XAxisRng As Range, ChtObj As ChartObject, Ser As Series
Set ChtObj = Worksheets(Counter).ChartObjects(Chart) 'Chart is the name of the chart. I have 12 charts that I'm updating
ActiveSheet.ChartObjects(Chart).Activate
With Sheets(1)
Set XAxisRng = .Range("P5", .Cells(5, .Columns.Count).End(xlToLeft))
End With
With ChtObj
Set Ser = .Chart.SeriesCollection(Coll) 'It errors on this line with Invalid Parameter
Ser.XValues = "=" & XAxisRng.Address(True, True, xlA1, xlExternal)
End With
End If
它运行到此行Set Ser = .Chart.SeriesCollection(1)
,然后我收到错误:
无效参数
我确实尝试使用数字代替系列集合编号的变量但仍然得到错误。
更新2
以下是我的全部代码:
Sub Macro5()
Dim Counter, Chart, ChartNum
Dim C As Range, B As Range, A As Integer
Dim r1, r2, MultipleRange As Range
Counter = 1
For x = Counter To 4 Step 1
ChartNum = 15
Chart = "Chart " & ChartNum
Application.Worksheets(Counter).Activate
'A = Range("A2") 'This is not needed
'Set C = ActiveSheet.Cells(5, ActiveSheet.Columns.Count).End(xlToLeft) 'ActiveCell
'C.Select 'This will show you what is being set to C
Set B = ActiveSheet.Cells(6, ActiveSheet.Columns.Count).End(xlToLeft) 'This is giving you the last column in row 6
'B.Select 'This will show you what is being set to B
If Counter > 1 And Count < 4 Then
ChartNum = ChartNum - 2
End If
If Counter < 4 Then
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=Range("P5", B)
Set r1 = ActiveSheet.Cells(5, ActiveSheet.Columns.Count).End(xlToLeft) 'C 'C is not needed anywhere and neither is B
Set r2 = ActiveSheet.Cells(7, ActiveSheet.Columns.Count).End(xlToLeft) 'This is giving you the last column in row 6
ChartNum = ChartNum + 1
If ChartNum = 14 Then
ChartNum = ChartNum + 1
End If
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P7", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
Set r2 = ActiveSheet.Cells(8, ActiveSheet.Columns.Count).End(xlToLeft)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P8", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
Set r2 = ActiveSheet.Cells(9, ActiveSheet.Columns.Count).End(xlToLeft)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P9", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
Set r2 = ActiveSheet.Cells(10, ActiveSheet.Columns.Count).End(xlToLeft)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P10", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
Set r2 = ActiveSheet.Cells(11, ActiveSheet.Columns.Count).End(xlToLeft)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P11", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
End If
If Counter = 1 Then
Set r2 = ActiveSheet.Cells(12, ActiveSheet.Columns.Count).End(xlToLeft)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P12", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
Set r2 = ActiveSheet.Cells(13, ActiveSheet.Columns.Count).End(xlToLeft)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P13", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
Set r2 = ActiveSheet.Cells(14, ActiveSheet.Columns.Count).End(xlToLeft)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P14", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
Set r2 = ActiveSheet.Cells(15, ActiveSheet.Columns.Count).End(xlToLeft)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P15", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
Set r2 = ActiveSheet.Cells(16, ActiveSheet.Columns.Count).End(xlToLeft)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P16", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
Set r2 = ActiveSheet.Cells(17, ActiveSheet.Columns.Count).End(xlToLeft)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
Set MultipleRange = Application.Union(Range("P5", r1), Range("P17", r2))
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SetSourceData Source:=MultipleRange
End If
If Counter = 4 Then
Dim XAxisRng As Range, ChtObj As ChartObject, Ser As Series ', Coll
Set ChtObj = Worksheets(Counter).ChartObjects(Chart)
'Coll = 1
ActiveSheet.ChartObjects(Chart).Activate
With Sheets(1)
Set XAxisRng = .Range("P5", .Cells(5, .Columns.Count).End(xlToLeft))
End With
With ChtObj
Set Ser = .Chart.SeriesCollection(1)
Ser.XValues = "=" & XAxisRng.Address(True, True, xlA1, xlExternal)
'Ser.Values = "=" & Sheets(1).Range("P6:" & GetLetterFromNumber(Sheets(1).Cells(6, Sheets(1).Columns.Count).End(xlToLeft).Column) & "6").Address(True, True, xlAl, xlExternal)
'Set Ser = .Chart.SeriesCollection(Coll + 1)
'Ser.Values = "=" & Sheets(1).Range("P6:" & GetLetterFromNumber(Sheets(1).Cells(6, Sheets(1).Columns.Count).End(xlToLeft).Column) & "6").Address(True, True, xlAl, xlExternal)
'Set Ser = .Chart.SeriesCollection(Coll + 2)
'Ser.Values = "=" & Sheets(1).Range("P6:" & GetLetterFromNumber(Sheets(1).Cells(6, Sheets(1).Columns.Count).End(xlToLeft).Column) & "6").Address(True, True, xlAl, xlExternal)
End With
'ActiveSheet.ChartObjects(Chart).Chart.SeriesCollection(1).XValues = "=" & XAxisRng.Address(True, True, xlA1, xlExternal)
'ActiveChart.SeriesCollection(1).Values = "=" & Sheets(1).Range("P6:" & GetLetterFromNumber(Sheets(1).Cells(6, Sheets(1).Columns.Count).End(xlToLeft).Column) & "6").Address(True, True, xlAl, xlExternal)
'ActiveChart.SeriesCollection(2).Values = "=" & Sheets(2).Range("P6:" & GetLetterFromNumber(Sheets(2).Cells(6, Sheets(2).Columns.Count).End(xlToLeft).Column) & "6").Address(True, True, xlAl, xlExternal)
'ActiveChart.SeriesCollection(3).Values = "=" & Sheets(3).Range("P6:" & GetLetterFromNumber(Sheets(3).Cells(6, Sheets(3).Columns.Count).End(xlToLeft).Column) & "6").Address(True, True, xlAl, xlExternal)
ChartNum = ChartNum + 3
Chart = "Chart " & ChartNum
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SeriesCollection(1).Values = "=" & Sheets(1).Range("P7:" & GetLetterFromNumber(Sheets(1).Cells(7, Sheets(1).Columns.Count).End(xlToLeft).Column) & "7").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(2).Values = "=" & Sheets(2).Range("P7:" & GetLetterFromNumber(Sheets(2).Cells(7, Sheets(2).Columns.Count).End(xlToLeft).Column) & "7").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(3).Values = "=" & Sheets(3).Range("P7:" & GetLetterFromNumber(Sheets(3).Cells(7, Sheets(3).Columns.Count).End(xlToLeft).Column) & "7").Address(True, True, xlAl, xlExternal)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SeriesCollection(1).Values = "=" & Sheets(1).Range("P8:" & GetLetterFromNumber(Sheets(1).Cells(8, Sheets(1).Columns.Count).End(xlToLeft).Column) & "8").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(2).Values = "=" & Sheets(2).Range("P8:" & GetLetterFromNumber(Sheets(2).Cells(8, Sheets(2).Columns.Count).End(xlToLeft).Column) & "8").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(3).Values = "=" & Sheets(3).Range("P8:" & GetLetterFromNumber(Sheets(3).Cells(8, Sheets(3).Columns.Count).End(xlToLeft).Column) & "8").Address(True, True, xlAl, xlExternal)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SeriesCollection(1).Values = "=" & Sheets(1).Range("P9:" & GetLetterFromNumber(Sheets(1).Cells(9, Sheets(1).Columns.Count).End(xlToLeft).Column) & "9").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(2).Values = "=" & Sheets(2).Range("P9:" & GetLetterFromNumber(Sheets(2).Cells(9, Sheets(2).Columns.Count).End(xlToLeft).Column) & "9").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(3).Values = "=" & Sheets(3).Range("P9:" & GetLetterFromNumber(Sheets(3).Cells(9, Sheets(3).Columns.Count).End(xlToLeft).Column) & "9").Address(True, True, xlAl, xlExternal)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SeriesCollection(1).Values = "=" & Sheets(1).Range("P10:" & GetLetterFromNumber(Sheets(1).Cells(10, Sheets(1).Columns.Count).End(xlToLeft).Column) & "10").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(2).Values = "=" & Sheets(2).Range("P10:" & GetLetterFromNumber(Sheets(2).Cells(10, Sheets(2).Columns.Count).End(xlToLeft).Column) & "10").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(3).Values = "=" & Sheets(3).Range("P10:" & GetLetterFromNumber(Sheets(3).Cells(10, Sheets(3).Columns.Count).End(xlToLeft).Column) & "10").Address(True, True, xlAl, xlExternal)
ChartNum = ChartNum + 1
Chart = "Chart " & ChartNum
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.SeriesCollection(1).Values = "=" & Sheets(1).Range("P11:" & GetLetterFromNumber(Sheets(1).Cells(11, Sheets(1).Columns.Count).End(xlToLeft).Column) & "11").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(2).Values = "=" & Sheets(2).Range("P11:" & GetLetterFromNumber(Sheets(2).Cells(11, Sheets(2).Columns.Count).End(xlToLeft).Column) & "11").Address(True, True, xlAl, xlExternal)
ActiveChart.SeriesCollection(3).Values = "=" & Sheets(3).Range("P11:" & GetLetterFromNumber(Sheets(3).Cells(11, Sheets(3).Columns.Count).End(xlToLeft).Column) & "11").Address(True, True, xlAl, xlExternal)
End If
Counter = Counter + 1
Next x
'Workbooks("Name").Activate
End Sub
Function GetLetterFromNumber(Number)
GetLetterFromNumber = Split(Cells(1, Number).Address(True, False), "$")(0)
End Function
Function GetArray(Range1 As Range)
Dim Test() As String, cell As Range, i As Integer
i = 0
ReDim Test(0)
For Each cell In Range1
ReDim Preserve Test(i)
Test(i) = cell.Value
i = i + 1
Next cell
GetArray = Test
End Function
大部分工作正常。它到达If Counter = 4 Then
下面的行,它出错了。该行是:
Set Ser = .Chart.SeriesCollection(1)
错误是:
无效参数
有什么东西我错过了让我搞砸了吗?
答案 0 :(得分:0)
尝试以下代码:
Dim XAxisRng As Range
With Sheets(1)
Set XAxisRng = .Range("P5", .Cells(5, .Columns.Count).End(xlToLeft))
End With
ActiveChart.SeriesCollection(1).XValues = "=" & XAxisRng.Address(True, True, xlA1, xlExternal)
再试一次,这次避免使用ActiveChart
,并使用完全限定的变量和对象。
Dim ChtObj As ChartObject
Dim XAxisRng As Range
Dim Ser As Series
If Counter = 4 Then
Set ChtObj = ActiveSheet.ChartObjects("Chart 1") '<-- modify "Chart 1" to your chart's name
With Sheets(1)
Set XAxisRng = .Range("P5", .Cells(5, .Columns.Count).End(xlToLeft))
End With
With ChtObj
Set Ser = .Chart.SeriesCollection(1)
Ser.XValues = "=" & XAxisRng.Address(True, True, xlA1, xlExternal)
' rest of your code goes here
End With
' other code you might have
End If ' closing If Counter = 4