Attribute VB_Name = "CodigosComunes" ' Gerencie.com - Códigos de macros más comunes en Excel. ' Importar en el editor de VBA (Alt + F11): Archivo > Importar archivo. ' Cada macro es un ejemplo independiente; ejecútelas en un libro de prueba. ' 1. Ir a una celda y escribir en ella Sub EscribirEnCelda() Range("A1").Value = "Ventas 2026" Range("B1").Formula = "=SUM(B2:B10)" ' las fórmulas se escriben en inglés y con coma End Sub ' 2. Formato de fuente Sub FormatoFuente() With Selection.Font .Bold = True .Italic = False .Underline = xlUnderlineStyleSingle .Name = "Calibri" .Size = 12 .Color = RGB(31, 56, 100) End With End Sub ' 3. Alineación y relleno Sub AlinearYRellenar() With Selection .HorizontalAlignment = xlCenter ' xlLeft, xlRight .VerticalAlignment = xlCenter .Interior.Color = RGB(255, 242, 204) .NumberFormat = "#,##0" End With End Sub ' 4. Copiar y pegar solo valores Sub PegarValores() Range("A1:C10").Copy Range("E1").PasteSpecial Paste:=xlPasteValues Application.CutCopyMode = False End Sub ' 5. Ordenar un rango con encabezado por la columna A Sub OrdenarAscendente() Range("A1").CurrentRegion.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes End Sub ' 6. Buscar un texto y avisar si no se encuentra Sub BuscarTexto() Dim c As Range Set c = Cells.Find(What:="Ramón", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False) If c Is Nothing Then MsgBox "No se encontró." Else c.Select End If End Sub ' 7. Insertar y eliminar filas y columnas Sub InsertarEliminar() Rows(5).Insert Rows(5).Delete Columns("C").Insert Columns("C").Delete End Sub ' 8. Borrar contenido, formatos o todo Sub Borrar() Range("F2:H4").ClearContents ' solo el contenido; conserva el formato ' Range("F2:H4").ClearFormats ' solo el formato ' Range("F2:H4").Clear ' contenido y formato ' Range("F2:H4").ClearComments ' solo los comentarios ' Range("F2:H4").Hyperlinks.Delete ' solo los hipervínculos End Sub ' 9. Borrar el texto de las celdas seleccionadas que tengan más de 60 caracteres Sub BorrarTextosLargos() Dim c As Range For Each c In Selection If Len(c.Value) > 60 Then c.ClearContents Next c End Sub ' 10. Última fila con datos y primera celda vacía de la columna A Sub UltimaFila() Dim u As Long u = Cells(Rows.Count, "A").End(xlUp).Row MsgBox "Última fila con datos: " & u Cells(u + 1, "A").Select End Sub ' 11. Recorrer un rango y marcar los valores negativos Sub MarcarNegativos() Dim c As Range For Each c In Range("B2:B100") If IsNumeric(c.Value) And c.Value < 0 Then c.Font.Color = vbRed Next c End Sub ' 12. Pedir un dato y confirmar Sub PedirDato() Dim nombre As String nombre = InputBox("Escriba el nombre del cliente") If nombre = "" Then Exit Sub If MsgBox("¿Agregar a " & nombre & "?", vbYesNo) = vbYes Then Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Value = nombre End If End Sub ' 13. Hojas: agregar, cambiar el nombre, ir a una hoja y ocultarla Sub Hojas() Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Resumen" Worksheets("Resumen").Activate ' Worksheets("Resumen").Visible = xlSheetHidden End Sub ' 14. Proteger y desproteger la hoja Sub ProtegerHoja() ActiveSheet.Protect Password:="1234" ' ActiveSheet.Unprotect Password:="1234" End Sub ' 15. Abrir un libro y guardarlo como .xlsx Sub AbrirYGuardar() Dim lb As Workbook Set lb = Workbooks.Open(Filename:="C:\Documentos\Ventas.xlsx") lb.SaveAs Filename:="C:\Documentos\Ventas copia.xlsx", FileFormat:=51 lb.Close SaveChanges:=False End Sub ' 16. Acelerar macros largas Sub MacroRapida() Application.ScreenUpdating = False Application.Calculation = xlCalculationManual ' ... instrucciones ... Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub