Gráfico mediante VBA Excel.
Hola tengo la siguiente pregunta. Ejecute la grabadora de macros para que me otrgue el código para graficar las notas de un curso en un periodo dado.. Al momento de adjudicar el código al botón el compilador me dice que existe un error en la linea de código:
ActiveChart.Location Where:=xlLocationAsObject, Name:="Grafico de Promedios Anual"
¿Cuál sera el inconveniente?
El código completo es el siguiente:
Sub NEOGRAFICO()
Static promedios(22 To 26, 5 To 5)
Dim i, j As Variant
j = 5
For i = 22 To 26
If Hoja6.Cells(i, j) = "D" Then
promedios(i, j) = 1
Hoja6.Cells(i, 6) = promedios(i, j)
ElseIf Hoja6.Cells(i, j) = "I" Then
promedios(i, j) = 2
Hoja6.Cells(i, 6) = promedios(i, j)
ElseIf Hoja6.Cells(i, j) = "A" Then
promedios(i, j) = 3
Hoja6.Cells(i, 6) = promedios(i, j)
ElseIf Hoja6.Cells(i, j) = "S" Then
promedios(i, j) = 4
Hoja6.Cells(i, 6) = promedios(i, j)
ElseIf Hoja6.Cells(i, j) = "E" Then
promedios(i, j) = 5
Hoja6.Cells(i, 6) = promedios(i, j)
End If
Next i
'
' NEOGRAFICO Macro
'
Range("D22:G26").Select
Charts.Add
' ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:="Tubos"
ActiveChart.SetSourceData Source:=Sheets("Grafico de Promedios").Range( _
"D22:G26"), PlotBy:=xlColumns
ActiveChart.SeriesCollection(2).Delete
ActiveChart.SeriesCollection(1).XValues = "='Grafico de Promedios'!R7C4:R11C4"
ActiveChart.SeriesCollection(1).Values = "='Grafico de Promedios'!R7C5:R11C5"
ActiveChart.SeriesCollection(1).Name = "=""Grafico Anual de Promedios"""
' ActiveChart.Location Where:=xlLocationAsObject, Name:="Grafico de Promedios Anual"
ActiveChart.Location Where = xlLocationAsObject
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Grafico Anual (CURSO-PROMEDIO)"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "CURSO"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "PROMEDIO"
End With
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
ActiveChart.HasDataTable = False
ActiveChart.Legend.Select
Selection.Delete
End Sub
ActiveChart.Location Where:=xlLocationAsObject, Name:="Grafico de Promedios Anual"
¿Cuál sera el inconveniente?
El código completo es el siguiente:
Sub NEOGRAFICO()
Static promedios(22 To 26, 5 To 5)
Dim i, j As Variant
j = 5
For i = 22 To 26
If Hoja6.Cells(i, j) = "D" Then
promedios(i, j) = 1
Hoja6.Cells(i, 6) = promedios(i, j)
ElseIf Hoja6.Cells(i, j) = "I" Then
promedios(i, j) = 2
Hoja6.Cells(i, 6) = promedios(i, j)
ElseIf Hoja6.Cells(i, j) = "A" Then
promedios(i, j) = 3
Hoja6.Cells(i, 6) = promedios(i, j)
ElseIf Hoja6.Cells(i, j) = "S" Then
promedios(i, j) = 4
Hoja6.Cells(i, 6) = promedios(i, j)
ElseIf Hoja6.Cells(i, j) = "E" Then
promedios(i, j) = 5
Hoja6.Cells(i, 6) = promedios(i, j)
End If
Next i
'
' NEOGRAFICO Macro
'
Range("D22:G26").Select
Charts.Add
' ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:="Tubos"
ActiveChart.SetSourceData Source:=Sheets("Grafico de Promedios").Range( _
"D22:G26"), PlotBy:=xlColumns
ActiveChart.SeriesCollection(2).Delete
ActiveChart.SeriesCollection(1).XValues = "='Grafico de Promedios'!R7C4:R11C4"
ActiveChart.SeriesCollection(1).Values = "='Grafico de Promedios'!R7C5:R11C5"
ActiveChart.SeriesCollection(1).Name = "=""Grafico Anual de Promedios"""
' ActiveChart.Location Where:=xlLocationAsObject, Name:="Grafico de Promedios Anual"
ActiveChart.Location Where = xlLocationAsObject
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Grafico Anual (CURSO-PROMEDIO)"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "CURSO"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "PROMEDIO"
End With
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
ActiveChart.HasDataTable = False
ActiveChart.Legend.Select
Selection.Delete
End Sub
1 Respuesta
Respuesta de Juan Carlos González Chavarría
1