|
Si tu agregas al final del código principal alguna línea del tipo de grafico que te gusto, ese se activara, por ejemplo:
SubMacro1()
Range("A5:B10").Select
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Hoja1").Range("A5:B10"), PlotBy:= _xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Hoja1"
ActiveChart.ChartType = xlPyramidColClustered.
End Sub
Este código se puede programar en un botón o cualquier otro control de Visual Basic.
A continuación se muestra como se acomodan los datos Línea 4:
ActiveChart.SetSourceData Source:=Sheets("Hoja1").Range("A5:B10"), PlotBy:= _
xlRows
En esta línea se muestra la grafica por Renglón
ActiveChart.SetSourceData Source:=Sheets("Hoja1").Range("A5:B10"), PlotBy:= _
xlColumns
En esta línea se muestra la grafica por Columna
Esta es la forma en que se muestran los datos de lo que habla la línea 4.
La línea 5 habla de que si la grafica queda en la misma hoja o simplemente toma una hoja para ella, por ejemplo:
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Grafico 1"
Esta línea indica que la grafica tenga su propia hoja y que su nombre sea Grafico 1.
En este ejemplo ejecuto un código con cada una de las características explicadas en las 5 líneas.
Range("A5:B10").Select
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Hoja1").Range("A5:B10"), PlotBy:= _xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Hoja1"
ActiveChart.ChartType = xlPyramidColClustered
ActiveChart.SetSourceData Source:=Sheets("Hoja1").Range("A5:B10"), PlotBy:= xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Grafico 1"
-
Tipo de Grafico
-
Como
se acomodan los datos
-
Como se muestra la grafica, en este caso en una sola hoja
Elabora el siguiente formulario con el siguiente código, para observar los diferentes tipos de gráficos y la forma en que se acomodan los datos:
Dibuja dos Listbox y un Botón y pega el código dentro del formulario.
Private Sub CommandButton1_Click()
Rem este código genera la Grafica en la hoja1
Range("A5:B10").Select
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Hoja1").Range("A5:B10"), PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Hoja1"
Rem agrega los diferentes tipos de grafica al Listbox1
ListBox1.AddItem "xlColumnClustered"
ListBox1.AddItem "xlBarClustered"
ListBox1.AddItem "xlLineMarkers"
ListBox1.AddItem "xlPie"
ListBox1.AddItem "xlXYScatter"
ListBox1.AddItem "xlAreaStacked"
ListBox1.AddItem "xlDoughnut"
ListBox1.AddItem "xlRadarMarkers"
ListBox1.AddItem "xlCylinderColClustered"
ListBox1.AddItem "xlConeColClustered"
ListBox1.AddItem "xlPyramidColClustered"
Rem agrega las diferentes formas de acomodar los datos al Listbox2
ListBox2.AddItem "Renglon"
ListBox2.AddItem "Columna"
End Sub
Private Sub ListBox1_Click()
Rem este código da el tipo de grafica al dar clic en el Listbox1
If ListBox1 = "xlColumnClustered" Then ActiveChart.ChartType = xlColumnClustered
If ListBox1 = "xlBarClustered" Then ActiveChart.ChartType = xlBarClustered
If ListBox1 = "xlLineMarkers" Then ActiveChart.ChartType = xlLineMarkers
If ListBox1 = "xlPie" Then ActiveChart.ChartType = xlPie
If ListBox1 = "xlXYScatter" Then ActiveChart.ChartType = xlXYScatter
If ListBox1 = "xlAreaStacked" Then ActiveChart.ChartType = xlAreaStacked
If ListBox1 = "xlDoughnut" Then ActiveChart.ChartType = xlDoughnut
If ListBox1 = "xlRadarMarkers" Then ActiveChart.ChartType = xlRadarMarkers
If ListBox1 = "xlCylinderColClustered" Then ActiveChart.ChartType = xlCylinderColClustered
If ListBox1 = "xlConeColClustered" Then ActiveChart.ChartType = xlConeColClustered
If ListBox1 = "xlPyramidColClustered" Then ActiveChart.ChartType = xlPyramidColClustered
End Sub
Private Sub ListBox2_Click()
If ListBox2 = "Renglon" Then
ActiveChart.SetSourceData Source:=Sheets("Hoja1").Range("A5:B10"), PlotBy:= _
xlRows
End If
If ListBox2 = "Columna" Then
ActiveChart.SetSourceData Source:=Sheets("Hoja1").Range("A5:B10"), PlotBy:= _
xlColumns
End If
End Sub
Antes de ejecutar esta Macro llenas los datos anteriores en la hoja1 de Excel
En este archivo indexo un ejemplo de un libro de cómo se puede generar una Macro que realice 25 graficas, esto por medio un modulo, este ejemplo es apoyado por su servidor hacia una persona que deseaba realizar este trabajo. Espero le sea de utilidad el código.
|
|
|
|
|
tutorial realizado por el Profesor Ramón Mendoza Ochoa
|
|