Documente Academic
Documente Profesional
Documente Cultură
DD-106
Página 1/31
Laboratorio – 02
OBJETIVOS
Identificar las principales herramientas para trabajar con macros que trae incorporado Microsoft Excel
Crear un documento en Excel que permita almacenar macros
Visualizar la ficha “Desarrollador” en la cinta de opciones de Excel
Grabar macros simples
Visualizar el código de una macro empleando la herramienta Visual Basic para Aplicaciones que viene
incorporado con Excel
REQUERIMIENTOS
PROCEDIMIENTO
1. Creación de macros en Excel
1.1. Crear un documento que permite almacenar Macros en Excel
Encienda la PC utilice la cuenta de usuario: tecsup clave: Virtu@l
Inicie el software MS Excel
Crear un nuevo libro y guardarlo con el nombre Lab_02 <nombre_completo>.xlsm en la carpeta de
la unidad E
2. Tipo de libro
1. Grabar el libro
Nota: Los archivos con extensión *.xlsx de Excel no permiten almacenar macros, para poder
grabar macros es necesario almacenar el archivo con la extensión *.xlsm como se indica
en el ítem anterior
Click derecho en la cinta de opciones como se indica en la figura anterior en alguna zona libre y luego
elija la opción “Personalizar la cinta de opciones”
1. Click
Derecho
Aquí
1. Seleccione
Desarrollador
2. Click
Botón
Aceptar
2
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 3/31
Laboratorio – 02
Haga Clic en la ficha Desarrollador y vea las herramientas que vienen disponibles:
A partir de este momento Excel “Grabará” cada una de las acciones que usted realice
Proceda a ingresar la palabra Mes en la celda A1. Al finalizar presione la tecla ENTER
Ahora procederemos a detener la grabación de la Macro anterior, para ello presione el botón
denominado “Detener grabación” ubicado en la categoría “Código” de la ficha “Desarrollador”:
3
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 4/31
Laboratorio – 02
Seleccione la única macro disponible hasta el momento denominada Macro1 y presione el botón
Ejecutar
Verifique que en la celda A1 de la Hoja 2 haya aparecido el texto Mes como resultado de la acción de
la macro
Repita el procedimiento anterior para que la macro coloque el texto Mes en la celda A1 de la Hoja 3
4
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 5/31
Laboratorio – 02
Del Panel izquierdo, denominado Proyecto, expanda el nodo Módulos y abra el archivo Módulo1
para ver su contenido:
El listado anterior representa el código de nuestra primera macro denominada Macro1 que fue
creada por nosotros previamente. Las instrucciones corresponden al lenguaje Visual Basic que es
utilizado por Excel para la programación de las macros.
2. Ejercicios propuestos de creación de macros en Excel
2.1. Macro para borrar el contenido de una celda
Grabar una nueva macro denominada m_borrar_celda que al ser ejecutada elimine el contenido de
la celda actual. Para ello siga los siguientes pasos: empiece una grabación nueva ingrese el
nombre propuesto de la macro cuando ya esté grabando la nueva macro, ubicarse en cualquier
celda borre su contenido con la tecla SUPR o DEL Detenga la grabación
Active la “Hoja 2”, coloque algún valor en la celda B3
Seleccione la celda B3 y ejecute la macro creada
Visualice el código generado para la nueva Macro y anote dicho código
Sub m_borrar_celda()
'
' m_borrar_celda Macro
'
'
Selection.ClearContents
End Sub
'
'
Range("B2").Select
ActiveCell.FormulaR1C1 = "=0.19*RC[-1]"
Range("B3").Select
End Sub
Range("A1:A10").Select
End Sub
'
Application.WindowState = xlMinimized
Application.WindowState = xlNormal
Range("A2").Select
ActiveCell.FormulaR1C1 = "Nro"
Range("B2").Select
ActiveCell.FormulaR1C1 = "Nombre"
Range("C2").Select
Application.WindowState = xlMinimized
Application.WindowState = xlNormal
Range("C2").Select
ActiveCell.FormulaR1C1 = "DNI"
Range("D2").Select
ActiveCell.FormulaR1C1 = "Sexo"
Range("E2").Select
Application.WindowState = xlMinimized
Application.WindowState = xlNormal
Range("E2").Select
ActiveCell.FormulaR1C1 = "Fecha de Nacimiento"
Range("E3").Select
Application.WindowState = xlMinimized
Application.WindowState = xlNormal
Range("E2").Select
Columns("E:E").ColumnWidth = 11.43
Rows("2:2").RowHeight = 29.25
Range("E2").Select
With Selection
.HorizontalAlignment = xlGeneral
7
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 8/31
Laboratorio – 02
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Application.WindowState = xlMinimized
Application.WindowState = xlNormal
Range("A2:E2").Select
Range("E2").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Application.WindowState = xlMinimized
Application.WindowState = xlNormal
Selection.Font.Bold = True
Application.WindowState = xlMinimized
Application.WindowState = xlNormal
Range("A2:E2").Select
Range("E2").Activate
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
8
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 9/31
Laboratorio – 02
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
Application.WindowState = xlMinimized
Application.WindowState = xlNormal
End Sub
9
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 10/31
Laboratorio – 02
Grabar una nueva macro denominada m_grafico_barras que al ser ejecutada genere un gráfico de
barras con los datos indicados en la hoja anterior
Ejecute la macro creada
Visualice el código generado para la nueva Macro y anote dicho código
Sub m_grafico_barras()
'
' m_grafico_barras Macro
'
'
Range("A2:A14,B2:B14").Select
Range("B2").Activate
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("Hoja3!$A$2:$A$14,Hoja3!$B$2:$B$14")
End Sub
10
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 11/31
Laboratorio – 02
Sub m_grafico_pie()
'
' m_grafico_pie Macro
'
'
Range("A2:A14,B2:B14").Select
Range("B2").Activate
ActiveSheet.Shapes.AddChart2(251, xlPie).Select
ActiveChart.SetSourceData Source:=Range("Hoja3!$A$2:$A$14,Hoja3!$B$2:$B$14")
ActiveChart.ClearToMatchStyle
ActiveChart.ChartStyle = 258
End Sub
Sub encabezado()
'
' Encabezado Macro
'
'
Range("A1").Select
ActiveCell.FormulaR1C1 = "Código"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Apellido"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Nombre"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Fecha de Nacimiento"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Área"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Fecha de contratación"
11
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 12/31
Laboratorio – 02
Range("G1").Select
ActiveCell.FormulaR1C1 = "Sueldo"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Ventas"
Range("A1:H1").Select
Selection.Font.Size = 12
Selection.Font.Bold = True
Rows("1:1").RowHeight = 32.25
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
12
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 13/31
Laboratorio – 02
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Columns("F:F").Select
Selection.ColumnWidth = 10.86
Columns("F:F").EntireColumn.AutoFit
Columns("D:D").ColumnWidth = 12
13
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 14/31
Laboratorio – 02
Range("A1").Select
End Sub
Sub dato_codigo()
'
' dato_codigo Macro
'
'
ActiveCell.FormulaR1C1 = "1001"
Range("A3").Select
ActiveCell.FormulaR1C1 = "1002"
Range("A2:A3").Select
Selection.AutoFill Destination:=Range("A2:A9"), Type:=xlFillDefault
Range("A2:A9").Select
End Sub
Sub datos_tabla()
'
' datos_tabla Macro
'
'
Range("H2").Select
ActiveCell.FormulaR1C1 = "24500"
Range("H3").Select
ActiveCell.FormulaR1C1 = "15000"
Range("H4").Select
ActiveCell.FormulaR1C1 = "7500"
Range("H5").Select
ActiveCell.FormulaR1C1 = "21000"
Range("H6").Select
ActiveCell.FormulaR1C1 = "15100"
Range("H7").Select
ActiveCell.FormulaR1C1 = "19600"
Range("H8").Select
ActiveCell.FormulaR1C1 = "8050"
Range("H9").Select
ActiveCell.FormulaR1C1 = "16500"
Range("G2").Select
ActiveCell.FormulaR1C1 = "2500"
Range("G3").Select
ActiveCell.FormulaR1C1 = "1500"
Range("G4").Select
ActiveCell.FormulaR1C1 = "1000"
14
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 15/31
Laboratorio – 02
Range("G5").Select
ActiveCell.FormulaR1C1 = "3000"
Range("G6").Select
ActiveCell.FormulaR1C1 = "4500"
Range("G7").Select
ActiveCell.FormulaR1C1 = "2000"
Range("G8").Select
ActiveCell.FormulaR1C1 = "1250"
Range("G9").Select
ActiveCell.FormulaR1C1 = "1950"
Range("G9").Select
Range("F2").Select
ActiveCell.FormulaR1C1 = "9/7/2007"
Range("F3").Select
ActiveCell.FormulaR1C1 = "5/4/2014"
Range("F4").Select
ActiveCell.FormulaR1C1 = "12/27/2013"
Range("F5").Select
ActiveCell.FormulaR1C1 = "12/27/2015"
Range("F6").Select
ActiveCell.FormulaR1C1 = "8/4/2012"
Range("F7").Select
ActiveCell.FormulaR1C1 = "9/1/2010"
Range("F8").Select
ActiveCell.FormulaR1C1 = "8/10/2015"
Range("F9").Select
ActiveCell.FormulaR1C1 = "11/4/2015"
Range("E2").Select
ActiveCell.FormulaR1C1 = "Contabilidad"
Range("E3").Select
ActiveCell.FormulaR1C1 = "Administracion"
Range("E4").Select
ActiveCell.FormulaR1C1 = "Operaciones"
Range("E5").Select
ActiveCell.FormulaR1C1 = "Administracion"
Range("E6").Select
ActiveCell.FormulaR1C1 = "Comercial"
Range("E7").Select
ActiveCell.FormulaR1C1 = "Sistemas"
Range("E8").Select
ActiveCell.FormulaR1C1 = "Produccion"
Range("E9").Select
ActiveCell.FormulaR1C1 = "Logistica"
Range("D2").Select
ActiveCell.FormulaR1C1 = "12/10/1995"
15
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 16/31
Laboratorio – 02
Range("D3").Select
ActiveCell.FormulaR1C1 = "7/23/1989"
Range("D4").Select
ActiveCell.FormulaR1C1 = "10/3/1991"
Range("D5").Select
ActiveCell.FormulaR1C1 = "1/26/1992"
Range("D6").Select
ActiveCell.FormulaR1C1 = "4/20/1988"
Range("D7").Select
ActiveCell.FormulaR1C1 = "8/16/1996"
Range("D8").Select
ActiveCell.FormulaR1C1 = "5/28/1986"
Range("D9").Select
ActiveCell.FormulaR1C1 = "9/12/1987"
Range("C2").Select
ActiveCell.FormulaR1C1 = "Ana"
Range("C3").Select
ActiveCell.FormulaR1C1 = "Juan"
Range("C4").Select
ActiveCell.FormulaR1C1 = "Carlos"
Range("C5").Select
ActiveCell.FormulaR1C1 = "Maria"
Range("C6").Select
ActiveCell.FormulaR1C1 = "Karen"
Range("C7").Select
ActiveCell.FormulaR1C1 = "Jorge"
Range("C8").Select
ActiveCell.FormulaR1C1 = "Pablo"
Range("C9").Select
ActiveCell.FormulaR1C1 = "Melchor"
Range("B2").Select
ActiveCell.FormulaR1C1 = "Salas Lima"
Range("B3").Select
ActiveCell.FormulaR1C1 = "Paz Andrade"
Range("B4").Select
ActiveCell.FormulaR1C1 = "Alvarez Diaz"
Range("B5").Select
ActiveCell.FormulaR1C1 = "Caceres Chavez"
Range("B6").Select
ActiveCell.FormulaR1C1 = "Miranda Gallegos"
Range("B7").Select
ActiveCell.FormulaR1C1 = "Zapata Torres"
Range("B8").Select
ActiveCell.FormulaR1C1 = "Zeballos Quispe"
Range("B9").Select
16
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 17/31
Laboratorio – 02
17
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 18/31
Laboratorio – 02
Sub edad()
'
' edad Macro
'
'
ActiveCell.FormulaR1C1 = "=INT((TODAY()-RC[-6])/365)"
Range("J2").Select
Selection.AutoFill Destination:=Range("J2:J9"), Type:=xlFillDefault
Range("J2:J9").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
18
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 19/31
Laboratorio – 02
.TintAndShade = 0
.Weight = xlThin
End With
End Sub
Sub fondo_pension()
'
' fondo_pension Macro
'
'
Range("K2:L2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.AutoFill Destination:=Range("K2:L9"), Type:=xlFillDefault
Range("K2:L9").Select
Range("K2:L2").Select
ActiveCell.FormulaR1C1 = "=+RC[-4]*0.13"
Range("K2:L2").Select
Selection.AutoFill Destination:=Range("K2:L9"), Type:=xlFillDefault
Range("K2:L9").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
19
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 20/31
Laboratorio – 02
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
20
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 21/31
Laboratorio – 02
End Sub
Sub essalud()
'
' essalud Macro
'
'
ActiveCell.FormulaR1C1 = ""
Range("M2:N2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Selection.AutoFill Destination:=Range("M2:N9"), Type:=xlFillDefault
Range("M2:N9").Select
Range("M2:N2").Select
ActiveCell.FormulaR1C1 = "=+RC[-6]*0.09"
Range("M2:N2").Select
Selection.AutoFill Destination:=Range("M2:N9"), Type:=xlFillDefault
Range("M2:N9").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
21
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 22/31
Laboratorio – 02
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End Sub
Sub sueldo_neto()
'
' sueldo_neto Macro
'
'
ActiveCell.FormulaR1C1 = "=+RC[-8]-RC[-4]-RC[-2]"
Range("O2").Select
Selection.AutoFill Destination:=Range("O2:O9"), Type:=xlFillDefault
Range("O2:O9").Select
Range("O4").Select
ActiveCell.FormulaR1C1 = "=+RC[-8]-RC[-4]-RC[-2]"
Range("O2:O9").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
22
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 23/31
Laboratorio – 02
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
23
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 24/31
Laboratorio – 02
End Sub
Sub tiempo_servicio()
'
' tiempo_servicio Macro
'
'
Range("C15:D15").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.AutoFill Destination:=Range("C15:D22"), Type:=xlFillDefault
Range("C15:D22").Select
Range("C15:D15").Select
ActiveCell.FormulaR1C1 = "=+INT((TODAY()-RC[-1])/365)"
Range("C15:D15").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
24
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 25/31
Laboratorio – 02
.MergeCells = True
End With
Selection.AutoFill Destination:=Range("C15:D22"), Type:=xlFillDefault
Range("C15:D22").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
25
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 26/31
Laboratorio – 02
End Sub
Sub situacion_trabajo()
'
' situacion_trabajo Macro
'
'
ActiveCell.FormulaR1C1 = "=+IF(RC[-1]>10000,""Promovido"",""Standby"")"
Range("F15").Select
Selection.AutoFill Destination:=Range("F15:F22"), Type:=xlFillDefault
Range("F15:F22").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
26
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 27/31
Laboratorio – 02
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
Sub m_imprimir()
'
' m_imprimir Macro
'
'
Range("A1:o22").Select
Range("o22").Activate
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
End Sub
Sub borrar_celda()
'
' borrar_celda Macro
'
'
Selection.ClearContents
End Sub
Sub grafico()
'
' grafico Macro
'
'
Range("H14:I22").Select
27
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 28/31
Laboratorio – 02
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range("Hoja1!$H$14:$I$22")
End Sub
Nota: Hemos creado el procedimiento con nombre “Sumarceldas” que permite sumar
dos valores específicos. Observar que el procedimiento empieza con el mensaje “Sub” y
termina con el mensaje “End Sub”.
ENTER
28
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 29/31
Laboratorio – 02
k. Dibujamos un botón cuadrado que será el botón para la suma. Seleccionamos el botón,
clic en propiedades.
29
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 30/31
Laboratorio – 02
m. Damos doble clic en el botón de suma, nos mostrará plataforma VBA – Libro1. En la
ventana de “cmdsuma”, escribimos “sumarceldas”. Luego damos doble clic a
Operacionesmatemáticas
2
n. Repetimos la operación para la resta.
Nota: Esta instrucción indica que al hacer doble clic en el botón creado, queremos utilizar
el procedimiento sumarceldas, creado en el módulo Operacionesmatemáticas.
p. Regresamos a Excel
q. En las celdas B2 y B3 insertamos dos valores al azar, damos clic en el botón suma y
comprobamos la suma.
r. Para detener la acción, estando en Excel nos dirigimos a VBA y seleccionamos la
opción de pausa o detención.
s. Realizar la misma operación para la resta, multiplicación y división:
En el módulo Operacionesmatematicas, agregar los procedimientos para la
resta, multiplicación y división (utilizar el mismo código que el de la suma,
realizando los cambios correspondientes).
En Excel crear tres botones de comando adicionales.
Cambiar sus nombres (name) y mensajes (caption) según corresponda.
Hacer doble clic en cada botón de comando generado. Utilizar el procedimiento
que corresponda para restar, multiplicar o dividir.
Probar el funcionamiento de cada botón.
30
EXCEL AVANZADO CON MACROS Nro. DD-106
Página 31/31
Laboratorio – 02
OBSERVACIONES Y CONCLUSIONES
El uso de las macros permiten automatizar y realizar tareas complejas, aumentando
la eficiencia y eficacia del trabajo.
Excel cuenta con comandos que hacen sencillo el proceso de aprender a crear macros y
ejecutarlas.
Una de las facilidades que ofrecen las macros es que se pueden crear sin saber programar
mediante la grabadora, la cual irá grabando todas las acciones que realicemos en Excel hasta
que detengamos la grabación y este se encargará de crear la Macro. De igual forma si ya se
tiene conocimientos de programación se puede digitar el código con las instrucciones que
serán ejecutadas por la macro.
El uso de macros son de gran ayuda para aprender a programar e idear un sinfín de
soluciones a problemas o tareas que se presenten en nuestro trabajo.
31