Está en la página 1de 5

CDIGOS MS COMUNES EN MACROS

Trasladarse a una Celda Range("A1").Select Escribir en una Celda Activecell.FormulaR1C1="Paty Acosta" Letra Negrita Selection.Font.Bold = True Letra Cursiva Selection.Font.Italic = True Letra Subrayada Selection.Font.Underline = xlUnderlineStyleSingle Centrar Texto With Selection .HorizontalAlignment = xlCenter End With Alinear a la izquierda With Selection .HorizontalAlignment = xlLeft End With Alinear a la Derecha With Selection .HorizontalAlignment = xlRight End With Tipo de Letra(Fuente) With Selection .Font .Name = "AGaramond" End With Tamao de Letra(Tamao de Fuente) With Selection.Font .Size = 15 End With Copiar Selection.Copy Pegar ActiveSheet.Paste Cortar Selection.CutOrdenar Ascendente Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom Orden Descendente Selection.Sort Key1:=Range("A1"), Order1:=xlDescending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom Buscar Cells.Find(What:="Paty Acosta", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False).ActivateInsertar Fila Selection.EntireRow.Insert

Eliminar Fila Selection.EntireRow.Delete Insertar Columna Selection.EntireColumn.Insert Eliminar Columna Selection.EntireColumn.Delete Abrir un Libro Workbooks.Open Filename:="C:\Mis documentos\miarchivo.xls"Grabar un Libro ActiveWorkbook.SaveAs Filename:="C:\Mis documentos\tauro.xls", FileFormat :=xlNormal, _ Password:="", WriteResPassword:="", ReadOnlyRecommended:= False, reateBackup:=False Case "N": ' Convertir a nmero Valor2 = Val(Valor2) Case "F": ' Convertir a Fecha Valor2 = CDate(Valor2) Ejemplo Sub Ejemplo_34() Dim x As Integer Dim n1 As Integer, n2 As Integer X = Suma(5, 5) n1= Val ( InputBox("Entrar un nmero : ", "Entrada")) n2= Val ( InputBox("Entrar otro nmero : ", "Entrada")) X= suma(n1,n2) ActiveCell.Value = Suma(ActiveSheet.Range("A1").Value , ActiveSheet.Range("A2").Value) X = Suma(5, 4) + Suma (n1, n2) End Sub Cerrar libro Excel (variable, sin guardar cambios) Application.DisplayAlerts = False Windows(Libro_mayor).Close Application.DisplayAlerts = True Abrir libro Excel (ruta fija) Workbooks.Open FileName:=C:\Trabajo\Informe.xls Desplazarnos a la ltima hoja del libro Sub ultima_hoja() Sheets(Sheets.Count).Select End Sub Desplazarnos a la primera hoja del libro Sub primera_hoja() Sheets(1).Select End Sub Macro para imprimir hoja activa con datos Sub macro imprimir_ha() Range(A1) = Lo que sea Imprimimos la hoja de excel con una sola copia ActiveWindow.SelectedSheets.PrintOut Copies:=1 End Sub Buscar la ltima fila vaca Sub ultimafila() variable donde almacenamos el nmero de fila Dim ultima As Long vamos subiendo por la columna A desde la ltima fila ultima = Range(A65536).End(xlUp).Row le sumamos una porque queremos la 1 fila vaca ultima = ultima + 1

seleccionamos si queremos otra columna cambiar el nmero Cells(ultima, 1).Select End Sub Encontrar ltima fila (en columna especificada) Dim intUltimaFila As Range If WorksheetFunction.CountA(Columns(1)) > 0 Then Set intUltimaFila = Range(65536).End(xlUp) MsgBox intUltimaFila.Address End If Suprimir filas vacas intLastRow = Columns(A:A).Range(A65536).End(xlUp).Row For r = intLastRow To 1 Step -1 If Application.CountA(Rows(r)) = 0 Then Rows(r).Delete Next r Suprimir filas vacas Dim intNumDeFilas As Long Selection.SpecialCells(xlCellTypeLastCell).Select intNumDeFilas = Selection.Row For i = 1 To intNumDeFilas If Application.WorksheetFunction.CountA(Rows(i)) = 0 Then Rows(i).Delete End If Next Suprimir filas vacas intUltimaFila = ActiveSheet.UsedRange.Row 1 + ActiveSheet.UsedRange.Rows.Count For r = intUltimaFila To 1 Step -1 If Application.CountA(Rows(r)) = 0 Then Rows(r).Delete Next r Suprimir filas por condicin Dim rngString As Range Do Set rngString = Cells.Find(Aglis, MatchCase:=False, _ LookAt:=xlPart, LookIn:=xlValues) If Not rngString Is Nothing Then rngString.EntireRow.Delete End If Loop Until rngString Is Nothing Suprimir filas vacas por dos condicines X, Y For i = intUltimaFila To 1 Step -1 Let strTest= Application.Cells(i, 2) If strTest <> X And strTest <> Y Then Rows(i).Delete Next i
Macro que elimina las barras de desplazamiento Sub elimina_barra() With ActiveWindow .DisplayHorizontalScrollBar = False .DisplayVerticalScrollBar = False End With End Sub Macro que protege el libro y la hoja Sub proteger() ActiveWorkbook.Protect Password:=1234 Sheets(Hoja1).Protect Password:=1234 End Sub

Calculo del NIF Esta funcin calcula el NIF (Nmero de Identificacin Fiscal). Function nif(dni As Long) As String nif = Mid(TRWAGMYFPDXBNJZSQVHLCKE, (dni Mod 23) + 1, 1) End Function

Alineacin izquierda/derecha
Sub Ajustar_izq_der() If Selection.HorizontalAlignment = xlRight Then Selection.HorizontalAlignment = xlLeft Else Selection.HorizontalAlignment = xlRight End If End Sub

Convertir pesetas a euro


Sub Convertir() Set Area = Selection For Each Cell In Area z = Round(Cell / 166.386, 2) Cell.Value = z Cell.NumberFormat = "#,##0.00" Next Cell End Sub

Pegar formato
Sub PegarFormato() Selection.PasteSpecial Paste:=xlFormats Application.CutCopyMode = False End Sub

Pegar valor
Sub PegarValor() Selection.PasteSpecial Paste:=xlValues Application.CutCopyMode = False End Sub

Dos decimales
Sub DosDec() Dim Area As Range Set Area = Selection For Each Cell In Area z = Round(Cell, 2) Cell.Value = z Cell.NumberFormat = "#,##0.00" Next Cell End Sub

Separador de miles
Sub SeparadorMil() Dim Area As Range Set Area = SelectionIf Area.NumberFormat = "#,##0" Then Area.NumberFormat = "#,##0.00"

Else Selection.NumberFormat = "#,##0" End If End Sub

Suprimir filas vacas


Sub SuprimirFilasVacias() LastRow = ActiveSheet.UsedRange.Row - 1 + _ ActiveSheet.UsedRange.Rows.Count For r = LastRow To 1 Step -1 If Application.CountA(Rows(r)) = 0 Then Rows(r).Delete End If Next r End Sub

Autofilter
Sub FilterExcel() Selection.AutoFilter End Sub

Grids (Lneas de divisin)


Sub Grids() If ActiveWindow.DisplayGridlines = True Then ActiveWindow.DisplayGridlines = False Else ActiveWindow.DisplayGridlines = True End If End Sub

Cambiar A1 a RC (columnas tiene nmeros en vez de letras)


Sub Rc() If Application.ReferenceStyle = xlR1C1 Then Application.ReferenceStyle = xlA1 Else Application.ReferenceStyle = xlR1C1 End If End Sub

Modificar paleta de colores


Sub ModificarPaleta() ActiveWindow.Zoom = 75 ActiveWorkbook.Colors(44) = RGB(236, 235, 194) ActiveWorkbook.Colors(40) = RGB(234, 234, 234) ActiveWorkbook.Colors(44) = RGB(236, 235, 194) End Sub

Mostrar todas las hojas


Sub MostrarHojas() Set wsHoja = Worksheets For Each wsHoja In ActiveWorkbook.Worksheets If wsHoja.Visible = False Then wsHoja.Visible = True End If Next wsHoja End Sub

También podría gustarte