Sub ProcesarEscandallo() Dim ws As Worksheet Dim LastRow As Long, i As Long, j As Long Dim UdsTotales() As Double Dim Nivel() As Integer Dim Tipos As Collection Dim TipoHoja As Worksheet Dim Tipo As Variant Dim wsOriginal As Worksheet ' Establecer la hoja original Set ws = ThisWorkbook.Sheets("Hoja1") LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Crear una nueva hoja para almacenar la tabla por defecto On Error Resume Next Application.DisplayAlerts = False Set wsOriginal = ThisWorkbook.Sheets("Por defecto") If Not wsOriginal Is Nothing Then wsOriginal.Delete End If Application.DisplayAlerts = True On Error GoTo 0 Set wsOriginal = ThisWorkbook.Sheets.Add(After:=ws) wsOriginal.Name = "Por defecto" ' Copiar la tabla original a la hoja "Por defecto" ws.Range(ws.Cells(1, 1), ws.Cells(LastRow, 11)).Copy Destination:=wsOriginal.Cells(1, 1) ' Crear nuevas columnas ws.Cells(1, 10).Value = "UDS EQ" ' Cambiar el nombre del encabezado ws.Cells(1, 11).Value = "OBSERVACIONES" ' Nueva columna OBSERVACIONES ' Formatear encabezados With ws.Range(ws.Cells(1, 10), ws.Cells(1, 11)) .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .Font.Bold = True End With ReDim UdsTotales(1 To LastRow) ReDim Nivel(1 To LastRow) ' Inicializar UDS TOTALES y calcular niveles jerárquicos For i = 2 To LastRow Nivel(i) = Len(ws.Cells(i, 1)) - Len(Replace(ws.Cells(i, 1), ".", "")) UdsTotales(i) = ws.Cells(i, 4).Value Next i ' Calcular UDS TOTALES basados en la jerarquía For i = LastRow To 2 Step -1 For j = i - 1 To 2 Step -1 If Nivel(j) < Nivel(i) Then UdsTotales(i) = UdsTotales(i) * UdsTotales(j) Exit For End If Next j Next i ' Colocar los valores calculados en la columna UDS EQ For i = 2 To LastRow ws.Cells(i, 10).Value = UdsTotales(i) Next i ' Asignar colores según el nivel jerárquico solo a las celdas con datos For i = 2 To LastRow Select Case Nivel(i) Case 1 ws.Range(ws.Cells(i, 1), ws.Cells(i, 11)).Interior.Color = RGB(255, 255, 153) ' Amarillo claro Case 2 ws.Range(ws.Cells(i, 1), ws.Cells(i, 11)).Interior.Color = RGB(204, 255, 204) ' Verde claro Case 3 ws.Range(ws.Cells(i, 1), ws.Cells(i, 11)).Interior.Color = RGB(204, 204, 255) ' Azul claro Case Else ws.Range(ws.Cells(i, 1), ws.Cells(i, 11)).Interior.Color = RGB(255, 204, 204) ' Rojo claro End Select Next i ' Formatear hoja inicial FormatearHoja ws, LastRow ' Crear una hoja nueva por cada tipo Set Tipos = New Collection On Error Resume Next For i = 2 To LastRow Tipos.Add ws.Cells(i, 6).Value, CStr(ws.Cells(i, 6).Value) Next i On Error GoTo 0 For Each Tipo In Tipos Set TipoHoja = ThisWorkbook.Sheets.Add On Error Resume Next TipoHoja.Name = Tipo ' Intentar nombrar la hoja según el tipo (si da error, se ignora) On Error GoTo 0 ' Copiar encabezados a la primera fila y copiar los datos debajo ws.Rows(1).Copy Destination:=TipoHoja.Rows(1) ' Formatear encabezados en la nueva hoja With TipoHoja.Range(TipoHoja.Cells(1, 1), TipoHoja.Cells(1, 11)) .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .Font.Bold = True End With j = 2 ' Iniciar en la fila 2 para los datos For i = 2 To LastRow If ws.Cells(i, 6).Value = Tipo Then ws.Rows(i).Copy Destination:=TipoHoja.Rows(j) j = j + 1 End If Next i ' Formatear hoja del tipo FormatearHoja TipoHoja, j - 1 Next Tipo ' Desplazar las tablas en todas las hojas Call DesplazarTablas ' Llamar a la subrutina para desplazar las tablas MsgBox "Proceso completado con éxito" End Sub Sub DesplazarTablas() Dim ws As Worksheet Dim LastRow As Long, LastCol As Long For Each ws In ThisWorkbook.Sheets ' Evitar la hoja "Por defecto" y desplazar "Hoja1" If ws.Name <> "Por defecto" Then ' Encontrar la última fila y columna con datos LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column ' Mover los datos 5 filas hacia abajo ws.Range(ws.Cells(1, 1), ws.Cells(LastRow, LastCol)).Cut Destination:=ws.Cells(6, 1) ' Formatear encabezados para mantener formato With ws.Rows(6) .Font.Bold = True .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .RowHeight = 52 ' Tamaño de fila para títulos End With End If Next ws End Sub Sub FormatearHoja(ByRef hoja As Worksheet, ByVal LastRow As Long) ' Aplicar bordes finos a todas las celdas con datos With hoja.Range(hoja.Cells(1, 1), hoja.Cells(LastRow, 11)) .Borders.LineStyle = xlContinuous .Borders.Weight = xlThin End With ' Aplicar bordes gruesos a las columnas With hoja.Range(hoja.Cells(1, 1), hoja.Cells(LastRow, 11)).Borders(xlEdgeLeft) .Weight = xlMedium End With With hoja.Range(hoja.Cells(1, 1), hoja.Cells(LastRow, 11)).Borders(xlEdgeRight) .Weight = xlMedium End With With hoja.Range(hoja.Cells(1, 1), hoja.Cells(LastRow, 11)).Borders(xlInsideVertical) .Weight = xlMedium End With ' Aplicar formato de fuente hoja.Cells.Font.Name = "Calibri" hoja.Cells.Font.Size = 15 ' Hacer títulos en negrita y ajustar tamaño de filas y columnas With hoja.Rows(1) .Font.Bold = True .RowHeight = 52 ' Tamaño de fila para títulos .HorizontalAlignment = xlCenter ' Alineación centrada horizontalmente .VerticalAlignment = xlCenter ' Alineación centrada verticalmente End With For i = 2 To LastRow hoja.Rows(i).RowHeight = 19.5 ' Tamaño de fila para datos Next i ' Ajustar ancho de columnas según especificaciones hoja.Columns(1).ColumnWidth = 18 hoja.Columns(2).ColumnWidth = 28 hoja.Columns(3).ColumnWidth = 44 hoja.Columns(4).ColumnWidth = 6 hoja.Columns(5).ColumnWidth = 6 hoja.Columns(6).ColumnWidth = 22 hoja.Columns(7).ColumnWidth = 22 hoja.Columns(8).ColumnWidth = 18 hoja.Columns(9).ColumnWidth = 6 hoja.Columns(10).ColumnWidth = 11.5 hoja.Columns(11).ColumnWidth = 40 End Sub