Sub txt_prova()
'crea file di testo da una cella dati excel
'dichiarazioni delle variabili
Dim Nomefile As String
Dim fs As Object
Dim a As Object
'variabili prima riga
Dim numero_invio As String
Dim data_invio As Date
Dim numero_sinistro As String
Dim numero_polizza As String
Dim luogo_sinistro As String
Dim provincia_sinistro As String
Dim data_sinistro As Date
Dim data_denuncia As Date
Dim cognome As String
Dim nome As String
Dim cf_pi As String
Dim targa As String
Dim importo_pagamento As Single
'variabili righe a metà
'variabili di comodo
Dim i, j, k As Integer
Dim n As Integer
Dim nrighe As Integer
'variabili date
Dim data_invio_trasformata As String
Dim data_sinistro_trasformata As String
Dim data_denuncia_trasformata As String
'costanti testata
Const tiporecord As String = "01"
Const mittente As String = "VTC"
'costanti corpo
Const tiporecord2 As String = "022016VTC"
Const garanzia As String = "D05"
Const tipo_operazione As String = "PT"
Const cognome_Vetrocar As String = "VETROCAR & BUS SPA"
Const pi_vetrocar As String = "03755190232"
Const iban_vetrocar As String = "IT04T0350059321000000011523"
'dichiaro le righe
n = 9
nrighe = Range("A9", Range("A9").End(xlDown)).Cells.Count
'salvataggio del file'
DesktopPath = Environ("userprofile") & "Desktop"
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(DesktopPath & "tracciato_assimoco" & ".txt", True)
'inserisco la prima riga
numero_invio = Cells(4, 1).Value
data_invio = Cells(4, 2).Value
'trasformo la data
data_invio_trasformata = Format(data_invio, "yyyy-mm-dd")
'sistemo le lunghezze
If Len(numero_invio) = 1 Then
numero_invio = "0000" & numero_invio
ElseIf Len(numero_invio) = 2 Then
numero_invio = "000" & numero_invio
ElseIf Len(numero_invio) = 3 Then
numero_invio = "00" & numero_invio
ElseIf Len(numero_invio) = 4 Then
numero_invio = "0" & numero_invio
ElseIf Len(numero_invio) = 5 Then
numero_invio = numero_invio
End If
'scrivo la prima linea
a.writeline tiporecord & mittente & numero_invio & data_invio_trasformata
'ciclo per il corpo del txt
For i = 1 To nrighe
numero_sinistro = Cells(n, 1).Value
numero_polizza = Cells(n, 2).Value
luogo_sinistro = Cells(n, 3).Value
provincia_sinistro = Cells(n, 4).Value
data_sinistro = Cells(n, 5).Value
data_denuncia = Cells(n, 6).Value
cognome = Cells(n, 7).Value
nome = Cells(n, 8).Value
cf_pi = Cells(n, 9).Value
targa = Cells(n, 10).Value
importo_pagamento = Cells(n, 12).Value
'sistemo la lunghezza del numero di sinistro
If Len(numero_sinistro) = 2 Then
numero_sinistro = "00000000" & numero_sinistro
ElseIf Len(numero_sinistro) = 3 Then
numero_sinistro = "0000000" & numero_sinistro
ElseIf Len(numero_sinistro) = 4 Then
numero_sinistro = "000000" & numero_sinistro
ElseIf Len(numero_sinistro) = 5 Then
numero_sinistro = "00000" & numero_sinistro
ElseIf Len(numero_sinistro) = 6 Then
numero_sinistro = "0000" & numero_sinistro
ElseIf Len(numero_sinistro) = 7 Then
numero_sinistro = "000" & numero_sinistro
ElseIf Len(numero_sinistro) = 8 Then
numero_sinistro = "00" & numero_sinistro
ElseIf Len(numero_sinistro) = 9 Then
numero_sinistro = "0" & numero_sinistro
ElseIf Len(numero_sinistro) = 10 Then
numero_sinistro = numero_sinistro
End If
l1 = 28
If Len(numero_sinistro) < l1 Then
numero_sinistro = numero_sinistro + Space(l1 - Len(numero_sinistro))
End If
'sistemo la lunghezza della polizza e la targa
l2 = 14
If Len(numero_polizza) < l2 Then
numero_polizza = numero_polizza + Space(l2 - Len(numero_polizza))
End If
If Len(targa) < l2 Then
targa = targa + Space(l2 - Len(targa))
End If
'sistemo la lunghezza di luogo sinistro, cognome e nome
l3 = 30
If Len(luogo_sinistro) < l3 Then
luogo_sinistro = luogo_sinistro + Space(l3 - Len(numero_polizza))
End If
If Len(cognome) < l3 Then
cognome = cognome + Space(l3 - Len(cognome))
End If
If Len(nome) < l3 Then
nome = nome + Space(l3 - Len(nome))
End If
'sistemo le date
data_sinistro_trasformata = Format(data_sinistro, "yyyy-mm-dd")
data_denuncia_trasformata = Format(data_denuncia, "yyyy-mm-dd")
'sistemo la lunghezza del codice fiscale o della partita iva
l4 = 16
If Len(cf_pi) < l4 Then
cf_pi = cf_pi + Space(l4 - Len(cf_pi))
End If
Next
'sistemo l'importo del pagamento
End Sub |