Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 声明两个非连续列作为带变量图表的轴_Excel_Vba - Fatal编程技术网

Excel 声明两个非连续列作为带变量图表的轴

Excel 声明两个非连续列作为带变量图表的轴,excel,vba,Excel,Vba,我制作了一个从表格中生成图表的电子表格。此表的大小可变,具体取决于我的条目。由于这种变化,我需要调整图形的比例 我制作了一个运行良好的初始VBA代码。当试图改进代码时,我想将此部分变成“for”,我在选择图形的X轴和Y轴时遇到困难。它给 错误424(运行时错误-对象是必需的) 我在问题出现的那一行之前加了一条评论 工作代码: ' Ajuste_Escala Macro ActiveSheet.ChartObjects("Gráfico 1").Activate ActiveSheet.Char

我制作了一个从表格中生成图表的电子表格。此表的大小可变,具体取决于我的条目。由于这种变化,我需要调整图形的比例

我制作了一个运行良好的初始VBA代码。当试图改进代码时,我想将此部分变成“for”,我在选择图形的X轴和Y轴时遇到困难。它给

错误424(运行时错误-对象是必需的)

我在问题出现的那一行之前加了一条评论

工作代码

' Ajuste_Escala Macro

ActiveSheet.ChartObjects("Gráfico 1").Activate
ActiveSheet.ChartObjects("Gráfico 1").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,D:D")

ActiveSheet.ChartObjects("Gráfico 2").Activate
ActiveSheet.ChartObjects("Gráfico 2").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,E:E")

ActiveSheet.ChartObjects("Gráfico 3").Activate
ActiveSheet.ChartObjects("Gráfico 3").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,F:F")

ActiveSheet.ChartObjects("Gráfico 4").Activate
ActiveSheet.ChartObjects("Gráfico 4").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,G:G")

ActiveSheet.ChartObjects("Gráfico 5").Activate
ActiveSheet.ChartObjects("Gráfico 5").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,H:H")

ActiveSheet.ChartObjects("Gráfico 6").Activate
ActiveSheet.ChartObjects("Gráfico 6").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,I:I")

ActiveSheet.ChartObjects("Gráfico 7").Activate
ActiveSheet.ChartObjects("Gráfico 7").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,J:J")

ActiveSheet.ChartObjects("Gráfico 8").Activate
ActiveSheet.ChartObjects("Gráfico 8").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,K:K")

ActiveSheet.ChartObjects("Gráfico 9").Activate
ActiveSheet.ChartObjects("Gráfico 9").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,L:L")

ActiveSheet.ChartObjects("Gráfico 10").Activate
ActiveSheet.ChartObjects("Gráfico 10").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,M:M")

ActiveSheet.ChartObjects("Gráfico 11").Activate
ActiveSheet.ChartObjects("Gráfico 11").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,N:N")

ActiveSheet.ChartObjects("Gráfico 12").Activate
ActiveSheet.ChartObjects("Gráfico 12").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,O:O")

ActiveSheet.ChartObjects("Gráfico 13").Activate
ActiveSheet.ChartObjects("Gráfico 13").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,P:P")

ActiveSheet.ChartObjects("Gráfico 14").Activate
ActiveSheet.ChartObjects("Gráfico 14").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,Q:Q")

Range("R1").Select
End Sub
' Ajuste_Escala Macro

Dim grafico As String
Dim graf As Integer
Dim numero As String
Dim nome As String
Dim colunaX As Integer
Dim colunaY As Integer
grafico = "Gráfico "
graf = 1
colunaX = 3
colunaY = 4


For I = 1 To 14

numero = CStr(graf)
nome = grafico + numero


ActiveSheet.ChartObjects(nome).Activate
ActiveChart.PlotArea.Select

' the next line is where the error occurs
ActiveChart.SetSourceData Source:=Union(Columns(colunaX), Columns(colunaY)).Select

graf = graf + 1
colunaY = colunaY + 1

Next I

Range("R1").Select

End Sub
不工作代码

' Ajuste_Escala Macro

ActiveSheet.ChartObjects("Gráfico 1").Activate
ActiveSheet.ChartObjects("Gráfico 1").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,D:D")

ActiveSheet.ChartObjects("Gráfico 2").Activate
ActiveSheet.ChartObjects("Gráfico 2").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,E:E")

ActiveSheet.ChartObjects("Gráfico 3").Activate
ActiveSheet.ChartObjects("Gráfico 3").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,F:F")

ActiveSheet.ChartObjects("Gráfico 4").Activate
ActiveSheet.ChartObjects("Gráfico 4").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,G:G")

ActiveSheet.ChartObjects("Gráfico 5").Activate
ActiveSheet.ChartObjects("Gráfico 5").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,H:H")

ActiveSheet.ChartObjects("Gráfico 6").Activate
ActiveSheet.ChartObjects("Gráfico 6").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,I:I")

ActiveSheet.ChartObjects("Gráfico 7").Activate
ActiveSheet.ChartObjects("Gráfico 7").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,J:J")

ActiveSheet.ChartObjects("Gráfico 8").Activate
ActiveSheet.ChartObjects("Gráfico 8").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,K:K")

ActiveSheet.ChartObjects("Gráfico 9").Activate
ActiveSheet.ChartObjects("Gráfico 9").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,L:L")

ActiveSheet.ChartObjects("Gráfico 10").Activate
ActiveSheet.ChartObjects("Gráfico 10").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,M:M")

ActiveSheet.ChartObjects("Gráfico 11").Activate
ActiveSheet.ChartObjects("Gráfico 11").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,N:N")

ActiveSheet.ChartObjects("Gráfico 12").Activate
ActiveSheet.ChartObjects("Gráfico 12").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,O:O")

ActiveSheet.ChartObjects("Gráfico 13").Activate
ActiveSheet.ChartObjects("Gráfico 13").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,P:P")

ActiveSheet.ChartObjects("Gráfico 14").Activate
ActiveSheet.ChartObjects("Gráfico 14").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("C:C,Q:Q")

Range("R1").Select
End Sub
' Ajuste_Escala Macro

Dim grafico As String
Dim graf As Integer
Dim numero As String
Dim nome As String
Dim colunaX As Integer
Dim colunaY As Integer
grafico = "Gráfico "
graf = 1
colunaX = 3
colunaY = 4


For I = 1 To 14

numero = CStr(graf)
nome = grafico + numero


ActiveSheet.ChartObjects(nome).Activate
ActiveChart.PlotArea.Select

' the next line is where the error occurs
ActiveChart.SetSourceData Source:=Union(Columns(colunaX), Columns(colunaY)).Select

graf = graf + 1
colunaY = colunaY + 1

Next I

Range("R1").Select

End Sub

我的目的不是添加任何新功能。我希望他们两人执行完全相同的任务,但是,我希望这是为了代码,因为这是一项重复的任务。

旁注:不需要
选择
,也不应该在
设置资源数据
行上有任何
选择
。^^^^^谢谢大家。现在开始工作了!