Gracias
Código: Seleccionar todo
Sub GrabarAsiento()
Dim Contador, UltimaLinea As Integer
Dim Asiento, Linea, LineaDestino, Ano As Long
Dim Fecha As Date
Dim Mes, Trimestre As String
'Primero mira a ver si el asiento está descuadrado
If Cells(5, 5) <> 0 Then
Mensajes 4
Exit Sub
End If
'Empieza al revés para determinar la última línea
Contador = 507
For Contador = 507 To 7 Step -1
If Cells(Contador, 2) <> "" Or CDbl(Cells(Contador, 3)) <> 0 Or CDbl(Cells(Contador, 4)) <> 0 Then
UltimaLinea = Contador
Exit For
End If
Next Contador
For Contador = 7 To UltimaLinea
If Cells(Contador, 2) = "" Then
Mensajes 5
Exit Sub
ElseIf Cells(Contador, 3) = "" And Cells(Contador, 4) = "" Then
Mensajes 6
Exit Sub
End If
Next Contador
If Cells(2, 2).Value = "" Then
Mensajes 7
Exit Sub
Else
Fecha = Format(Cells(2, 2).Value, "dd/mm/yyyy")
End If
'Cálculo Años, meses, trimestres
Ano = Format(Cells(2, 2).Value, "yyyy")
Mes = CStr(CInt(Format(Cells(2, 2).Value, "mm"))) & "M"
Trimestre = Format(Cells(2, 2).Value, "q") & "T"
'Si ha pasado todo esto, entonces es que ok
Asiento = Cells(2, 4).Value
Linea = 1
Contador = 1
'Obtiene la última línea de destino.
Do Until Worksheets("Diario").Cells(Contador, 1).Value = ""
Contador = Contador + 1
Loop
LineaDestino = Contador
For Contador = 7 To UltimaLinea
Worksheets("Diario").Cells(LineaDestino, 1).Value = Asiento
Worksheets("Diario").Cells(LineaDestino, 2).Value = Linea
Worksheets("Diario").Cells(LineaDestino, 3).Value = Fecha
Worksheets("Diario").Cells(LineaDestino, 4).Value = Cells(Contador, 2).Value
Worksheets("Diario").Cells(LineaDestino, 5).Value = Round(Cells(Contador, 3).Value, 2)
Worksheets("Diario").Cells(LineaDestino, 6).Value = Round(Cells(Contador, 4).Value, 2)
Worksheets("Diario").Cells(LineaDestino, 7).Value = Cells(3, 3).Value
Worksheets("Diario").Cells(LineaDestino, 8).Value = Cells(4, 3).Value
Worksheets("Diario").Cells(LineaDestino, 9).Value = Cells(Contador, 8).Value
Worksheets("Diario").Cells(LineaDestino, 10).Value = Cells(Contador, 6).Value
Worksheets("Diario").Cells(LineaDestino, 11).Value = Cells(Contador, 7).Value
Worksheets("Diario").Cells(LineaDestino, 12).Value = Cells(Contador, 9).Value
Worksheets("Diario").Cells(LineaDestino, 13).Value = Cells(Contador, 10).Value
Worksheets("Diario").Cells(LineaDestino, 14).Value = Left(Cells(Contador, 2).Value, 4)
Worksheets("Diario").Cells(LineaDestino, 15).Value = Left(Cells(Contador, 2).Value, 3)
Worksheets("Diario").Cells(LineaDestino, 16).Value = Left(Cells(Contador, 2).Value, 2)
Worksheets("Diario").Cells(LineaDestino, 17).Value = Left(Cells(Contador, 2).Value, 1)
Worksheets("Diario").Cells(LineaDestino, 18).Value = Ano
Worksheets("Diario").Cells(LineaDestino, 19).Value = Trimestre
Worksheets("Diario").Cells(LineaDestino, 20).Value = Mes
Linea = Linea + 1
LineaDestino = LineaDestino + 1
Next Contador
Range("B7:J507").ClearContents
Range("B2:B2").Select
Mensajes 8
End Sub