Tiempo de lectura: 1,5 minutos
Nivel de programación: iniciado
Útil para: muestreos de señales. Gráficas con gran número de cifras numéricas
Si tienes una gráfica con muchos datos en una columna, por ejemplo un muestreo
de una señal o miles de datos numéricos que quieras visualizar en
detalle, puedes crear un gráfico que se vaya adaptando a donde le vayas
indicando.
Con el siguiente código, puedes crear el interfaz y moverlo.
Solo tienes que tener una hoja de cálculo con dos hojas. Una llamada
Interfaz y una llamada Muestras.
En la hoja de Muestras, en la columna A1:A10000 estarán las muestras que
quieres visualizar.
Si corres la macro Crea_Interfaz desde la hoja Interfaz te crea los dos
gráficos, el principal y el de referencia, el cursor transparente
, las macros, los botones para moverla y unas celdas para cambiar manualmente
el inicio, el número de muestras total y el número de muestras a
visualizar.
Tiene un pequeño visor debajo del gráfico principal, para saber en qué
intervalo estás. Similar a los software de sonido.
Es bastante útil a mi parecer.
Queda así
El código es copiar, pegar y correr. Con la salvedad de tener las hojas y los
datos.
El símbolo + es un zoom para acercar la señal, es decir, rebajar el número de muestras del intervalo.
El símbolo - es para hacer zoom hacia arriba, mostrando más muestras en el intervalo.
El símbolo >>>>> es para desplazarse a la derecha el intervalo que indican las muestras
El símbolo <<<<< es para desplazarse a la izquierda el intervalo que indican las muestras
El botón Actualiza es para actualizar la muestra si modificas a mano las cifras.
Está construido de la siguiente manera.
Los botones de derecha e izquierda suman el intervalo de muestras que dice la celda y llaman a la función actualiza.
Los botones de zoom más y menos, recalculan el tamaño de la ventana de muestras y la posición y llaman a la función actualiza.
Las casillas modificables son C31, total de muestras D31, celda de inicio respecto a la número 1, E31 número de muestras a mostrar en el intervalo. La celda F31 es de información y no modifica el visor.
Se puede visualizar toda la muestra solo con los botones.
Si tiene algún glitch es cuestión de revisarlo. Este código no es comercial ni un producto terminado. Es un código para aprender los nombres de las variables, los objetos etc. Con la funcionalidad adicional que puedes visualizar gráficas. Significa que si escribes caracteres extraños en las celdas o cifras inconexas dará error.
Puedes modificar con relativa facilidad el tamaño del gráfico y el tipo
de gráfico. Relativamente fácil porque hay que meterse en el código y tocar. Si tienes cualquier duda deja un comentario y lo
resolvemos.
Si te gustaría que tuviese más funcionalidad, también.
Sub Crea_Interfaz()
Dim muestras As Integer
Dim mainchart_left As Integer
Dim mainchart_top As Integer
Dim mainchart_width As Integer
Dim mainchart_height As Integer
Dim referencechart_left As Integer
Dim referencechart_top As Integer
Dim referencechart_width As Integer
Dim referencechart_height As Integer
Dim boton_retroceso_top As Integer
Dim boton_retroceso_width As Integer
Dim boton_retroceso_height As Integer
Dim boton_retroceso_left As Integer
Dim boton_avance_top As Integer
Dim boton_avance_width As Integer
Dim boton_avance_height As Integer
Dim boton_avance_left As Integer
Dim boton_menos_top As Integer
Dim boton_menos_width As Integer
Dim boton_menos_height As Integer
Dim boton_menos_left As Integer
Dim boton_mas_top As Integer
Dim boton_mas_width As Integer
Dim boton_mas_height As Integer
Dim boton_mas_left As Integer
If Worksheets("Interfaz").Range("F31").Value = "Gráfico 1.0" Then
Exit Sub
End If
On Error GoTo errorhandler
Dim chart As ChartObject
Worksheets("Interfaz").Columns("A").ColumnWidth = 20
muestras = 10000
mainchart_left = 100
mainchart_top = 0
mainchart_width = 500
mainchart_height = 300
referencechart_width = mainchart_width
referencechart_height = 50
referencechart_left = mainchart_left
referencechart_top = mainchart_top + mainchart_height
Set chart = Worksheets("Interfaz").ChartObjects.Add(Left:=mainchart_left,Width:=mainchart_width, Top:=mainchart_top, Height:=mainchart_height)
chart.Name = "Chart1"
Set chart = Worksheets("Interfaz").ChartObjects.Add(Left:=mainchart_left, Width:=referencechart_width, Top:=referencechart_top, Height:=referencechart_height)
chart.Name = "Chart2"
muestras_inicio = muestras / 5
Worksheets("Interfaz").ChartObjects("Chart1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Values = "Muestras!$A$1:$A$" & muestras_inicio
ActiveChart.Legend.Select
Selection.Delete
Worksheets("Interfaz").ChartObjects("Chart2").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Values = "Muestras!$A$1:$A$" & muestras
ActiveChart.Axes(xlCategory).Select
Selection.Delete
ActiveChart.Legend.Select
Selection.Delete
boton_retroceso_top = referencechart_top + referencechart_height
boton_retroceso_left = referencechart_left
boton_retroceso_width = 100
boton_retroceso_height = 50
boton_avance_width = boton_retroceso_width
boton_avance_height = boton_retroceso_height
boton_avance_left = referencechart_left + referencechart_width - boton_avance_width
boton_avance_top = boton_retroceso_top
boton_menos_top = referencechart_top + referencechart_height
boton_menos_left = referencechart_left + boton_retroceso_width
boton_menos_width = boton_retroceso_width
boton_menos_height = boton_retroceso_height
boton_mas_width = boton_retroceso_width
boton_mas_height = boton_retroceso_height
boton_mas_width = boton_retroceso_width
boton_mas_height = boton_retroceso_height
boton_mas_top = referencechart_top + referencechart_height
boton_mas_left = referencechart_left + referencechart_width - boton_avance_width - boton_mas_width
boton_actualiza_width = boton_retroceso_width
boton_actualiza_height = boton_retroceso_height
boton_actualiza_top = referencechart_top + referencechart_height
boton_actualiza_left = referencechart_left + referencechart_width - boton_avance_width - boton_mas_width - boton_actualiza_width
boton_retroceso_top = referencechart_top + referencechart_height
boton_retroceso_width = boton_retroceso_width
Set boton = ActiveSheet.Shapes.AddShape(msoShapeBevel, boton_retroceso_left, boton_retroceso_top, boton_retroceso_width, boton_retroceso_height)
boton.Name = "boton_retroceso"
Set boton = ActiveSheet.Shapes.AddShape(msoShapeBevel,boton_avance_left, boton_avance_top, boton_avance_width, boton_avance_height)
boton.Name = "boton_avance"
Set boton = ActiveSheet.Shapes.AddShape(msoShapeBevel,boton_mas_left, boton_mas_top, boton_mas_width, boton_mas_height)
boton.Name = "boton_mas"
Set boton = ActiveSheet.Shapes.AddShape(msoShapeBevel,
boton_menos_left, boton_menos_top, boton_menos_width, boton_menos_height)
boton.Name = "boton_menos"
Set boton = ActiveSheet.Shapes.AddShape(msoShapeBevel,boton_actualiza_left, boton_actualiza_top,boton_actualiza_width,boton_actualiza_height)
boton.Name = "boton_actualiza"
ActiveSheet.Shapes.Range(Array("boton_mas")).Select
Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text =" +"
Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1,4).Font.Size = 32
Selection.ShapeRange(1).TextFrame2.TextRange.Characters(5,1).Font.Size = 40
ActiveSheet.Shapes.Range(Array("boton_menos")).Select
Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text =" -"
Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1,4).Font.Size = 32
Selection.ShapeRange(1).TextFrame2.TextRange.Characters(5,1).Font.Size = 40
ActiveSheet.Shapes.Range(Array("boton_avance")).Select
Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text =">>>>>>"
Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1,5).Font.Size = 40
ActiveSheet.Shapes.Range(Array("boton_retroceso")).Select
Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text ="<<<<<<"
Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1,5).Font.Size = 40
ActiveSheet.Shapes.Range(Array("boton_actualiza")).Select
Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = "Actualiza"
Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1,9).Font.Size = 20
Worksheets("Interfaz").Range("C30") = "Total Muestras"
Worksheets("Interfaz").Range("D30") = "Comenzar en"
Worksheets("Interfaz").Range("E30") = "Nº Muestras"
Worksheets("Interfaz").Range("C31") = muestras
Worksheets("Interfaz").Range("D31") = 1
Worksheets("Interfaz").Range("E31") = muestras_inicio
Worksheets("Interfaz").Range("F30").Value = "Centrado en"
Worksheets("Interfaz").Range("F31").Value = CInt(inicio + muestras_inicio / 2)
Worksheets("Interfaz").Range("G31") = "Visor 1.0"
Worksheets("Interfaz").Range("G31").Locked = True
cursor_left = mainchart_left + 30
cursor_width = mainchart_width / 5
cursor_top = referencechart_top
cursor_height = referencechart_height
Set cuadrado = ActiveSheet.Shapes.AddShape(msoShapeRectangle, cursor_left, cursor_top, cursor_width, cursor_height
cuadrado.Name = "Cursor"
'ActiveSheet.Shapes.Range(Array("Cursor")).ShapeRange.Fill.Transparency = 0.5
ActiveSheet.Shapes.Range(Array("Cursor")).Select
Application.CommandBars("Format Object").Visible = False
With Selection.ShapeRange.Fill
.Transparency = 0.5
End With
ActiveSheet.Shapes.Range(Array("boton_avance")).Select
Selection.OnAction = "Avanza_Interfaz"
ActiveSheet.Shapes.Range(Array("boton_retroceso")).Select
Selection.OnAction = "Retrocede_Interfaz"
ActiveSheet.Shapes.Range(Array("boton_mas")).Select
Selection.OnAction = "zoom_mas"
ActiveSheet.Shapes.Range(Array("boton_menos")).Select
Selection.OnAction = "zoom_menos"
ActiveSheet.Shapes.Range(Array("boton_actualiza")).Select
Selection.OnAction = "Actualiza_interfaz"
errorhandler:
End Sub
Sub Actualiza_Interfaz()
Dim tamaño_intervalo_pequeño As Double
Dim inicio_intervalo_pequeño As Double
Dim tamaño_intervalo_grande As Double
Dim inicio_intervalo_grande As Double
On Error GoTo errorhandler
limite_muestras = Worksheets("Interfaz").Range("C31").Value
muestras = Worksheets("Interfaz").Range("E31").Value
inicio = Worksheets("Interfaz").Range("D31").Value
tamaño_intervalo_grande = 460
inicio_intervalo_grande = 130
fin_intervalo_grande = inicio_intervalo_grande + tamaño_intervalo_grande
If inicio <= 0 Then
inicio = 1
End If
If muestras > limite_muestras Then
muestras = limite_muestras
End If
If muestras + inicio > limite_muestras Then
inicio = limite_muestras - muestras
End If
If inicio <= 0 Then
inicio = 1
End If
Inicio_chart = inicio
Fin_chart = Inicio_chart + muestras
b = "Muestras!$A$" & Inicio_chart & ":$A$" & Fin_chart
Worksheets("Interfaz").ChartObjects("Chart1").Activate
ActiveChart.FullSeriesCollection(1).Values = b
tamaño_intervalo_pequeño = tamaño_intervalo_grande * muestras / limite_muestras
inicio_intervalo_pequeño = (inicio / limite_muestras) * tamaño_intervalo_grande + inicio_intervalo_grande
Worksheets("Interfaz").Shapes.Range(Array("Cursor")).Width = tamaño_intervalo_pequeño
Worksheets("Interfaz").Shapes.Range(Array("Cursor")).Left = inicio_intervalo_pequeño
Worksheets("Interfaz").Range("F31").Value = CInt(inicio + muestras / 2)
errorhandler:
End Sub
Sub Avanza_Interfaz()
Dim limite_muestras As Integer
Dim muestras As Integer
Dim inicio As Integer
Dim Inicio_chart As String
Dim final As Integer
Dim final_chart As Integer
On Error GoTo errorhandler
limite_muestras = Worksheets("Interfaz").Range("C31").Value
muestras = Worksheets("Interfaz").Range("E31").Value
inicio = Worksheets("Interfaz").Range("D31").Value
If muestras > limite_muestras Or inicio >= limite_muestras Or muestras <= 0 Then
inicio = 1
muestras = limite_muestras
End If
If inicio < 0 Then
inicio = 1
End If
inicio = muestras + inicio
final = inicio + muestras
Inicio_chart = inicio
Fin_chart = final
If final > limite_muestras Then
final = limite_muestras
Fin_chart = final
inicio = limite_muestras - muestras
Inicio_chart = inicio
End If
Worksheets("Interfaz").Range("D31").Value = inicio
Worksheets("Interfaz").Range("E31").Value = muestras
Worksheets("Interfaz").Range("F31").Value = CInt(inicio + muestras / 2)
Call Actualiza_Interfaz
errorhandler:
End Sub
Sub Retrocede_Interfaz()
Dim limite_muestras As Integer
Dim muestras As Integer
Dim inicio As Integer
Dim Inicio_chart As Integer
Dim final As Integer
Dim final_chart As Integer
On Error GoTo errorhandler
limite_muestras = Worksheets("Interfaz").Range("C31").Value
muestras = Worksheets("Interfaz").Range("E31").Value
inicio = Worksheets("Interfaz").Range("D31").Value
If muestras > limite_muestras Or inicio >= limite_muestras
Or muestras <= 0 Then
inicio = 1
muestras = limite_muestras
End If
inicio = inicio - muestras
Inicio_chart = inicio
final = inicio + muestras
Fin_chart = final
If inicio < 0 Then
inicio = 1
Inicio_chart = 1
final = muestras
Fin_chart = final
End If
Worksheets("Interfaz").Range("D31").Value = inicio
Worksheets("Interfaz").Range("E31").Value = muestras
Worksheets("Interfaz").Range("F31").Value = CInt(inicio +
muestras / 2)
Call Actualiza_Interfaz
errorhandler:
End Sub
Sub zoom_mas()
Dim muestras As Integer
Dim inicio As Integer
Dim limite_muestras As Integer
On Error GoTo errorhandler
limite_muestras = Worksheets("Interfaz").Range("C31").Value
muestras = Worksheets("Interfaz").Range("E31").Value
inicio = Worksheets("Interfaz").Range("D31").Value
relacion = muestras / limite_muestras
If muestras > 1 Then
inicio = CInt(inicio + (muestras / 3))
muestras = CInt((muestras / 3))
Worksheets("Interfaz").Range("D31").Value = inicio
Worksheets("Interfaz").Range("E31").Value = muestras
Worksheets("Interfaz").Range("F31").Value = CInt(inicio + muestras / 2)
Call Actualiza_Interfaz
End If
errorhandler:
End Sub
Sub zoom_menos()
Dim muestras As Integer
Dim inicio As Integer
Dim limite_muestras As Integer
On Error GoTo errorhandler
limite_muestras = Worksheets("Interfaz").Range("C31").Value
muestras = Worksheets("Interfaz").Range("E31").Value
inicio = Worksheets("Interfaz").Range("D31").Value
relacion = muestras / limite_muestras
If relacion < 1 Then
inicio = CInt(inicio - (muestras))
muestras = CInt(muestras * 3)
If muestras + inicio > limite_muestras Then
inicio = limite_muestras - muestras
End If
If inicio <= 0 Then
inicio = 1
End If
Worksheets("Interfaz").Range("D31").Value = inicio
Worksheets("Interfaz").Range("E31").Value = muestras
Worksheets("Interfaz").Range("F31").Value = CInt(inicio + muestras / 2)
Call Actualiza_Interfaz
End If
errorhandler:
End Sub
Comentarios
Publicar un comentario