You are on page 1of 2

Sub nivelar()

codigo = Cells(2, 1)
i = 2
nfilas = 1294
coldif = 15
tra105 = 16
t105al = 20
salcl = 17
incl = 18
ajust = 19
Do While i < nfilas
suma = 0
sumap = 0
suman = 0
j = 0
Do While Cells(i, 1) = codigo
'columna 19 debe estar la diferencia
If Cells(i, coldif) > 0 Then
'columna 26 debe estar la transferencia al 105
Cells(i, tra105) = Cells(i, coldif)
sumap = sumap + Cells(i, coldif)
Else
Cells(i, t105al) = -1 * Cells(i, coldif)
suman = suman - 1 * Cells(i, coldif)
End If
suma = suma + Cells(i, coldif)
i = i + 1
j = j + 1
Cells(2, 31) = i
Cells(2, 32) = j
Loop
'saldo positivo
If suma > 0 Then
Rept = sumap - suma
Cells(2, 33) = Rept
k = i - j + 1
For n = k - 1 To i
If Cells(n, coldif) > 0 Then
If Cells(n, coldif) < Rept Then
Cells(n, salcl) = Cells(n, coldif)
Rept = Rept - Cells(n, coldif)
Else
Cells(n, salcl) = Rept
Exit For
End If
End If
Next n
k = i - j + 1
Do While k < i
If Cells(k - 1, t105al) > 0 Then
Cells(k - 1, incl) = Cells(k - 1, t105al)
End If
k = k + 1
Loop
'saldo negativo
Else
k = i - j + 1

For n = k - 1 To i
If Cells(n, coldif) > 0 Then
Cells(n, salcl) = Cells(n, coldif)
End If
Next
For n = k - 1 To i
If Cells(n, t105al) > 0 Then
If sumap > Cells(n, t105al) Then
Cells(n, incl) = Cells(n, t105al)
sumap = sumap - Cells(n, t105al)
Else
Cells(n, incl) = sumap
Exit For
End If
End If
Next
For n = k - 1 To i
Cells(n, ajust) = Cells(n, t105al) - Cells(n, incl)
Next
End If
codigo = Cells(i, 1)
Loop
End Sub

You might also like