Sub ConsolidarVentasMensuales() Dim ws As Worksheet Dim wsTotal As Worksheet Dim nombreHojaTotal As String Dim ultimaFila As Long Dim ultimaFilaTotal As Long Dim rngOrigen As Range Dim encabezadosCopiados As Boolean Dim nombreHoja As String ' --- CONFIGURACIÓN --- nombreHojaTotal = "Total" encabezadosCopiados = False ' Optimizar velocidad Application.ScreenUpdating = False Application.Calculation = xlCalculationManual ' 1. Crear o limpiar la hoja "Total" On Error Resume Next Set wsTotal = Sheets(nombreHojaTotal) On Error GoTo 0 If wsTotal Is Nothing Then Set wsTotal = Sheets.Add(Before:=Sheets(1)) wsTotal.Name = nombreHojaTotal Else wsTotal.Cells.Clear End If ' 2. Recorrer todas las hojas del libro For Each ws In ThisWorkbook.Worksheets nombreHoja = ws.Name ' 3. Validar que no sea una hoja a ignorar If nombreHoja <> nombreHojaTotal And _ nombreHoja <> "Configuración" And _ nombreHoja <> "Listas" And _ nombreHoja <> "Dashboard" Then ' Buscar última fila con datos en la hoja mensual (columna A) ultimaFila = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Verificar si la hoja tiene datos (más allá del encabezado) If ultimaFila > 1 Then ' 4. Copiar encabezados una sola vez If Not encabezadosCopiados Then ws.Rows(1).Copy Destination:=wsTotal.Rows(1) ' Agregar columna "Mes" al final del encabezado wsTotal.Cells(1, ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column + 1).Value = "Mes" encabezadosCopiados = True End If ' 5. Copiar datos y agregar nombre del Mes ultimaFilaTotal = wsTotal.Cells(wsTotal.Rows.Count, "A").End(xlUp).Row + 1 ' Definir rango de datos (sin encabezado) Set rngOrigen = ws.Range("A2", ws.Cells(ultimaFila, ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column)) ' Pegar valores y formatos rngOrigen.Copy wsTotal.Cells(ultimaFilaTotal, 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats ' Llenar la columna "Mes" para los registros recién pegados wsTotal.Range(wsTotal.Cells(ultimaFilaTotal, wsTotal.Cells(1, wsTotal.Columns.Count).End(xlToLeft).Column), _ wsTotal.Cells(wsTotal.Cells(wsTotal.Rows.Count, "A").End(xlUp).Row, _ wsTotal.Cells(1, wsTotal.Columns.Count).End(xlToLeft).Column)).Value = nombreHoja End If End If Next ws ' 6. Formato Final If encabezadosCopiados Then ultimaFilaTotal = wsTotal.Cells(wsTotal.Rows.Count, "A").End(xlUp).Row ' Ajustar ancho de columnas wsTotal.Columns.AutoFit ' Aplicar formato moneda (suponiendo columnas de dinero al final o específicas) ' En este ejemplo buscamos encabezados con "USD" o "Precio" Dim cell As Range For Each cell In wsTotal.Range(wsTotal.Cells(1, 1), wsTotal.Cells(1, wsTotal.Columns.Count).End(xlToLeft)) If InStr(1, cell.Value, "USD") > 0 Or InStr(1, cell.Value, "Precio") > 0 Then wsTotal.Columns(cell.Column).NumberFormat = "$#,##0.00" End If Next cell ' 7. Totales Generales ' Buscamos la columna de "Ventas_USD" para sumar Dim colVentas As Long, colCant As Long, colComision As Long On Error Resume Next colVentas = wsTotal.Rows(1).Find("Ventas_USD").Column colCant = wsTotal.Rows(1).Find("Unidades").Column ' o "Cantidad" colComision = wsTotal.Rows(1).Find("Comisión_USD").Column On Error GoTo 0 ultimaFilaTotal = ultimaFilaTotal + 2 wsTotal.Cells(ultimaFilaTotal, 1).Value = "TOTALES GENERALES:" wsTotal.Cells(ultimaFilaTotal, 1).Font.Bold = True If colVentas > 0 Then wsTotal.Cells(ultimaFilaTotal, colVentas).FormulaR1C1 = "=SUM(R2C:R[-2]C)" wsTotal.Cells(ultimaFilaTotal, colVentas).Font.Bold = True End If ' Convertir a tabla oficial de Excel para mejor análisis Dim ultimaCol As Long: ultimaCol = wsTotal.Cells(1, wsTotal.Columns.Count).End(xlToLeft).Column wsTotal.ListObjects.Add(xlSrcRange, wsTotal.Range(wsTotal.Cells(1, 1), wsTotal.Cells(ultimaFilaTotal - 2, ultimaCol)), , xlYes).Name = "TablaVentasConsolidadas" Else MsgBox "No se encontraron hojas mensuales con datos.", vbExclamation End If ' Restaurar configuración Application.CutCopyMode = False Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True MsgBox "Consolidación completada con éxito en la hoja 'Total'.", vbInformation End Sub