You are on page 1of 6

RELACION DE MACROS VBA MS UTILIZADAS EN EXCEL

El formador dice y el estudiante hace


Abra el Editor de Visual Basic para aplicaciones y realice estas macros aplicadas tanto a libros como a rangos de hojas

Para localizar la ltima celda de una lista


Sub Final_lista() Range (A1).Select While ActiveCell.Value <> "" ActiveCell.Offset(1, 0).Select Wend End Sub

Manejo de Rangos de tablas Para sealar una tabla se hace con CurrentRegion. Un ejemplo: Sub Marcar_tabla() Dim R As Range Dim filas As Long Set R = Range("A1").CurrentRegion filas = R.Rows.Count MsgBox Nro. Filas : & & filas End Sub Macro que reemplaza una palabra por otra en todas las hojas Sub RemplazarPalabra() Dim Palabra As String Dim Hoja As Long Palabra = Trim(InputBox("Introduzca la palabra a buscar: ")) For Hoja = 1 To Sheets.Count Sheets(Hoja).Activate Cells.Replace What:=Palabra, Replacement:="" Next Hoja En este caso reemplazamos por "", lo que supone borrar la palabra buscada. End Sub .

Aplica un color en funcin del valor de la celda


Sub colores_celda() Dim Zonamodificar As Range Dim celda As Range Set Zonamodificar = Selection For Each celda In Zonamodificar celda = InputBox("DIGITE VALOR NUMERICO") Select Case celda

Formador: Heladio Polo Castro - Instituto Colombiano de Aprendizaje - INCAP

RELACION DE MACROS VBA MS UTILIZADAS EN EXCEL


Case Is < 1000 celda.Font.Color = RGB(150, 150, 250) Case Is < 5000 celda.Font.Color = RGB(90, 100, 250) Case Is < 2000 celda.Font.Color = RGB(10, 20, 250) Case Else celda.Font.Color = RGB(5, 5, 100) End Select Next celda End Sub Sub formato_personal() ' formato de celdas para un rango Set rangox = Selection With Selection.Font .Name = "Algerian" .Size = 14 .Italic = False .Bold = True End With With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom Selection.NumberFormat = "$ #,##0.00" End With End Sub Sub insertar_fila() ActiveCell.Select Selection.Insert End Sub Sub eliminar_fila() ActiveCell.Select Selection.Delete End Sub Sub insertahoja_alfinal() Sheets.Add after:=Sheets(Sheets.Count) End Sub Sub cerrar_libro() ActiveWorkbook.Save ActiveWorkbook.Close End Sub

Formador: Heladio Polo Castro - Instituto Colombiano de Aprendizaje - INCAP

RELACION DE MACROS VBA MS UTILIZADAS EN EXCEL


Sub imprimir_copas() Dim numero As Integer ActiveSheet.PrintPreview numero = InputBox("DIGITE CUANTAS COPIAS QUIERE", " NUMERO DE COPIAS") ActiveSheet.PrintOut copies:=numero End Sub Sub anade_registro() 'Crea primero una tabla con 5 nombres de personas en la columna A :NOMBRE Dim filalibre As Integer Range("A1").Select While ActiveCell.Value <> "" ActiveCell.Offset(1, 0).Select Wend filalibre = ActiveCell.Row Range("A" & filalibre).Select End Sub Sub Filtrar() Range("A1").Select Selection.AutoFilter End Sub

Sub limpia_celdas() Selection.Clear End Sub Sub copiar_rango() Set rangox = Selection Selection.Copy Sheets("hoja3").Select Range("a3").Select ActiveSheet.Paste End Sub Sub buscar_datos() Dim buscar As String buscar = InputBox(" ESCRIBE EL TEXTO: ", "BUSCAR TEXTO") Range("A1").Select While ActiveCell.Value <> "" If ActiveCell.Value <> buscar Then ActiveCell.Offset(1, 0).Select Else MsgBox "NOMBRE ENCONTRADO" Exit Sub

Formador: Heladio Polo Castro - Instituto Colombiano de Aprendizaje - INCAP

RELACION DE MACROS VBA MS UTILIZADAS EN EXCEL


End If Wend MsgBox "ERROR...NOMBRE NO ENCONTRADO" End Sub Sub desproteger_rango() 'Sombrea primero un rango de 5 celdas en una hoja Set rangop = Selection For Each cell In rangop cell.Locked = False Next cell ActiveSheet.Protect "hpolo" End Sub Sub filtrar_dato() Filtra el nombre de cliente seleccionado por el usuario Dim dato As String Range("a3:e8").Select Selection.AutoFilter dato = InputBox("ESCRIBA EL DATO A FILTRAR EN: (NOMBRE CLIENTE)") Selection.AutoFilter FIELD:=1, Criteria1:=dato Range(Selection, Selection.End(xlDown)).Select Selection.Copy Sheets.Add Range("a1").PasteSpecial xlPasteValues Application.CutCopyMode = False Sheets("HOJA1").Select End Sub

Sub BUSCA_REEMPLAZA() ' Primero hay que seleccionar el rango de texto a aplicar BSQUEDA Dim sw As Integer Dim txtbus As String Dim txtreemp As String sw = 0 Set RANGOBUSCAR = Selection ' DIGITE EL TEXTO A BUSCAR Y EL TEXTO A REEMPLAZAR txtbus = InputBox("DIGITE EL CLIENTE A BUSCAR", "BUSCAR INFORMACION") txtreemp = InputBox(" DIGITE POR CUAL NOMBRE VA A REEMPLAZAR", "REEMPLAZAR") Application.ScreenUpdating = False 'EN ESTA INSTRUCCION RECORRE CELDA POR CELDA DEL RANGO SELECCIONADO _ Y AL LOCALIZARLA LA REEMPLAZA For Each CELDA In RANGOBUSCAR If CELDA.Value = txtbus Then CELDA.Value = Replace(CELDA.Value, txtbus, txtreemp) sw = 1

Formador: Heladio Polo Castro - Instituto Colombiano de Aprendizaje - INCAP

RELACION DE MACROS VBA MS UTILIZADAS EN EXCEL


Exit For End If Next CELDA Application.ScreenUpdating = True ' AQUI MANDA UN MENSAJE POR PANTALLA DE PROCESO EXITOSO If sw <> 0 Then MsgBox " CLIENTE REEMPLAZADO", vbExclamation Else MsgBox " CLIENTE NO ENCONTRADO", vbCritical End If End Sub Sub BUSCA_ELIMINA() ' Primero hay que seleccionar el rango de texto a aplicar Dim sw As Integer Dim nempresa As String sw = 0 Application.ScreenUpdating = False Application.Goto ActiveWorkbook.Worksheets("HOJA1").Range("a3") ' PIDE EL CLIENTE A BUSCAR Y ELIMINAR nempresa = InputBox("DIGITE EL NOMBRE DEL CLIENTE A ELIMINAR", "ELIMINAR CLIENTE") 'AQUI SE ENCARGA DE ENCONTRAR EL CLIENTE EN LA TABLA If nempresa = "" Then MsgBox " CLIENTE NO ELIMINADO", vbCritical, "ERROR" Else While ActiveCell.Value <> "" If ActiveCell.Value = nempresa Then Selection.EntireRow.Delete sw = 1 Else ActiveCell.Offset(1, 0).Select End If Wend ' AQUI MANDA UN MENSAJE POR PANTALLA CUANDO NO SE ENCUENTRA If sw = 0 Then MsgBox " CLIENTE NO LOCALIZADO", vbCritical, "ERROR" End If End If Application.ScreenUpdating = True End Sub Sub ordenar() Dim rang As Range Set rang = Selection ' Ordena la tabla por el primer campo ej: Nombre _ en forma ascendente alfabtica la tabla comienza desde la fila 3 columna A rang.Sort key1:=Range("A3"), order1:=xlAscending, _

Formador: Heladio Polo Castro - Instituto Colombiano de Aprendizaje - INCAP

RELACION DE MACROS VBA MS UTILIZADAS EN EXCEL


Header:=xlYes End Sub Sub borra_linea_vacia() Dim fila As Integer Dim fin_fila As Integer fin_fila = InputBox("Digite Nro. ltima fila Hoja ") For fila = 1 To fin_fila If Cells(fila, 1).Value = 0 Then Rows(fila).Delete End If Next fila End Sub Sub agruparhojas() Sheets(Array("Hoja1", "Hoja2", "Hoja3")).Select Sheets("Hoja2").Activate End Sub Sub AreaImpresion() Dim primera, ultima As Variant Range("A1").Select primera = ActiveCell.Address ActiveCell.SpecialCells(xlLastCell).Select ultima = ActiveCell.Address ActiveSheet.PageSetup.PrintArea = (primera & ":" & ultima) End Sub Sub rangear_copiar_datos() 'Los ttulos de la tabla comienzan desde la celda A3 Range("A3").Select Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select Selection.Copy 'Ahora a pegar datos abajo en la misma hoja Range("A10").Select ActiveCell.Offset(1, 0).Range("a1").Select ActiveSheet.Paste Application.CutCopyMode = False Range("a1").Select End Sub Luego como siguiente paso en una hoja de Excel dentro del libro, inserta Botones de comando (Tipo formulario) para asignar a ellos las macros creadas por usted, los mismos deben de ser aplicados en el lugar correcto dentro del libro.

Formador: Heladio Polo Castro - Instituto Colombiano de Aprendizaje - INCAP

You might also like