I'm trying to convert financial MT940 file to workbook with VBA, but I have some problems.
This is my unfinished code:
MsgBoxes are using for debugging purpose (do I cut proper text fragment).
I could not publish the original input file, because he contains confidential financial information.
This is my unfinished code:
Rich (BB code):
Option Explicit
Public Referencja As String, IBAN As String, Wyciag As String, Nazwa_skr As String, Pole_salda As String
Public Saldo_poczatkowe As Currency, Saldo_typ As String * 1, Saldo_data_ks As Date, Saldo_waluta As String * 3
Sub Alior_Read_MT940()
Application.DecimalSeparator = ","
ActiveWorkbook.RefreshAll
Dim i As Integer, PATH As String, FileList()
PATH = ThisWorkbook.Sheets("SET").Range("A2").Value
If Right(PATH, 1) <> "\" Then PATH = PATH & "\"
FileList = Worksheets("SET").ListObjects("Pliki").DataBodyRange.Value
For i = LBound(FileList, 1) To UBound(FileList, 1)
If FileList(i, 1) <> "" Then Call SplitFile(FileList(i, 1), PATH)
Next i
End Sub
Sub SplitFile(ByVal FileName As String, ByVal PATH As String)
Application.DecimalSeparator = ","
Dim char_1
Dim MT_file As String, text As String
MT_file = "" & PATH & FileName
Open MT_file For Input Access Read As #1
Do Until EOF(1)
char_1 = Input(1, #1)
text = text & char_1
Loop
Close #1
MsgBox ("tekst: " & text)
'!!! NAGŁÓWEK !!!
Dim parametry(5) As String, pola_gł(5) As String, i As Integer
Dim koniec_linii(5) As Long, początek_linii(5) As Long, dł_linii(5) As Long
parametry(1) = ":20:"
parametry(2) = ":25:"
parametry(3) = ":28C:"
parametry(4) = ":NS:22"
parametry(5) = ":60F:"
For i = 1 To UBound(parametry)
If i = 1 Then
początek_linii(i) = InStr(text, parametry(i)) + CInt(Len(parametry(i)))
koniec_linii(i) = InStr(text, Chr$(13))
dł_linii(i) = koniec_linii(i) - początek_linii(i)
pola_gł(i) = Mid(text, początek_linii(i), dł_linii(i))
Else
If InStr(text, parametry(i)) = 0 Then
początek_linii(i) = początek_linii(i - 1)
koniec_linii(i) = koniec_linii(i - 1)
pola_gł(i) = ""
Else
początek_linii(i) = InStr(koniec_linii(i - 1) + 2, text, parametry(i)) + CInt(Len(parametry(i)))
koniec_linii(i) = InStr(koniec_linii(i - 1) + 2, text, Chr$(13))
dł_linii(i) = koniec_linii(i) - początek_linii(i)
pola_gł(i) = Mid(text, początek_linii(i), dł_linii(i))
End If
End If
Next i
If Not IsEmpty(Pole_salda) Then
Saldo_typ = Left(Pole_salda, 1)
Saldo_data_ks = DateSerial(Val("20" & Mid(Pole_salda, 2, 2)), Val(Mid(Pole_salda, 4, 2)), Val(Mid(Pole_salda, 6, 7)))
Saldo_waluta = Mid(Pole_salda, 8, 3)
Saldo_poczatkowe = CCur(Mid(Pole_salda, 11))
End If
Referencja = pola_gł(1)
IBAN = pola_gł(2)
Wyciag = pola_gł(3)
Nazwa_skr = pola_gł(4)
Pole_salda = pola_gł(5)
MsgBox ("Referencja: " & Referencja)
MsgBox ("IBAN: " & IBAN)
MsgBox ("Wyciąg: " & Wyciag)
MsgBox ("Nazwa skrócona" & Nazwa_skr)
MsgBox ("Pole salda: " & Pole_salda)
MsgBox ("Typ salda: " & Saldo_typ)
MsgBox ("Data księgowania: " & Saldo_data_ks)
MsgBox ("Waluta: " & Saldo_waluta)
MsgBox ("Saldo początkowe: " & Saldo_poczatkowe)
End Sub
MsgBoxes are using for debugging purpose (do I cut proper text fragment).
I could not publish the original input file, because he contains confidential financial information.