Back to Browse

16 Ciclo Do While en VBA Excel

7 views
May 16, 2026
17:54

Sub CargarCamion() Dim fila As Integer Dim acumulado As Double Dim pesoSiguiente As Double Const LIMITE_MAXIMO As Double = 1000 fila = 2 acumulado = 0 ' Limpiamos la columna de estado antes de empezar Range("C2:C100").ClearContents ' El bucle corre MIENTRAS haya paquetes en la lista Do While Cells(fila, 1).Value mM "" pesoSiguiente = Cells(fila, 2).Value ' CONTROL DE SOBRECARGA: Validamos antes de sumar If (acumulado + pesoSiguiente) m= LIMITE_MAXIMO Then ' El paquete cabe perfectamente acumulado = acumulado + pesoSiguiente Cells(fila, 3).Value = "EMBARCADO" Cells(fila, 3).Font.Color = vbGreen Else ' El paquete no cabe Cells(fila, 3).Value = "RECHAZADO (Excede límite)" Cells(fila, 3).Font.Color = vbRed ' Nota: Dejamos el bucle abierto por si los siguientes paquetes son más ligeros y sí caben End If ' Si llegamos al límite exacto, forzamos la salida del bucle If acumulado = LIMITE_MAXIMO Then Exit Do fila = fila + 1 Loop ' Mensaje final detallado para el jefe de almacén MsgBox "--- RESUMEN DE EMBARQUE ---" & vbCrLf & _ "Peso Total Cargado: " & acumulado & " kg." & vbCrLf & _ "Capacidad Utilizada: " & Format((acumulado / LIMITE_MAXIMO), "0.0%") & vbCrLf & _ "Espacio remanente: " & (LIMITE_MAXIMO - acumulado) & " kg.", _ vbInformation, "Logística de Despacho" End Sub

Download

0 formats

No download links available.

16 Ciclo Do While en VBA Excel | NatokHD