I made a spreadsheet that generates graphs from a table. This table has a variable size, depends on my entry. Due to this variation, I need to adjust the scale of the graphics.
I made an initial VBA code that works well. When trying to improve the code, I would like to turn this section into a "for", I am having difficulty in selecting the X and Y axes of the graph. It gives
error 424 (Runtime error - The object is mandatory)
I put a comment before the line on which the problem manifests itself.
Working Code:
' 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
Not Working Code:
' 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
My intention is not to add any new functionality. I want them both to perform exactly the same task, however, I would like it to be in for code, since it is a repetitive task.