Вы находитесь на странице: 1из 8

Sub abrir_ofx(arquivo As String, ARQ_P As String)

Dim ARQ_S, PLANILHA As String

Workbooks.OpenText Filename:=arquivo, _

Origin:=xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:= _

Array(0, 1), TrailingMinusNumbers:=True

ARQ_S = ActiveWorkbook.Name

Columns("A:A").Select

Selection.Copy

Windows(ARQ_P).Activate

Sheets("AUX").Select

Range("A1").Select

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

:=False, Transpose:=False

Application.CutCopyMode = False

Sheets("Extrato").Select

Windows(ARQ_S).Activate

ActiveWindow.Close

End Sub

Sub indicar_arquivo()

Dim intChoice As Integer

Dim strPath, ARQ_P As String

With Application

.ScreenUpdating = False
End With

ARQ_P = ActiveWorkbook.Name

'only allow the user to select one file

Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False

'Remove all other filters

Call Application.FileDialog(msoFileDialogOpen).Filters.Clear

'Add a custom filter

Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _

"Apenas OFX", "*.ofx,*.ofc", 1)

'make the file dialog visible to the user

intChoice = Application.FileDialog(msoFileDialogOpen).Show

'determine what choice the user made

If intChoice <> 0 Then

'get the file path selected by the user

strPath = Application.FileDialog( _

msoFileDialogOpen).SelectedItems(1)

Sheets("AUX").Select

Columns("A:A").Select

Selection.ClearContents

Range("A1").Select

Call abrir_ofx((strPath), (ARQ_P))

Call limpa_extrato

Call organiza_ofx

End If

With Application

.ScreenUpdating = True
End With

End Sub

Sub organiza_ofx()

Dim linha_ini, i, cont As Integer

Dim tagOFX, banco, agencia, conta, tipo_conta, dataOFX, tipo_transacao, valor_transacao, ref_transacao,
memo_transacao, valor_saldo_final As String

Dim data_ini, data_fim, data_transacao As Date

Dim vlr, vlrSdFinal As Double

'linha inicial do EXTRATO

cont = 7

'Fazer aqui uma limpa no que tem no EXTRATO

'bla bla bla pra limpra

'Definir onde começa OFX

For i = 1 To 100

aux = Sheets("AUX").Cells(i, 1).Value

If aux = "<OFX>" Then Exit For

Next i

Do While aux <> ""

'pega Banco

tagOFX = "<ORG>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

banco = Mid(aux, Len(tagOFX) + 1, Len(aux) - ((Len(tagOFX) * 2) + 1))

Sheets("Extrato").Cells(2, 4).Value = banco

End If
'pega agencia <BRANCHID>

tagOFX = "<BRANCHID>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

agencia = Mid(aux, Len(tagOFX) + 1, Len(aux) - ((Len(tagOFX) * 2) + 1))

Sheets("Extrato").Cells(3, 4).Value = agencia

End If

'pega conta

tagOFX = "<ACCTID>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

conta = Mid(aux, Len(tagOFX) + 1, Len(aux) - ((Len(tagOFX) * 2) + 1))

Sheets("Extrato").Cells(4, 4).Value = conta

End If

'pega tipo de conta

'tagOFX = "<ACCTTYPE>"

'If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

' tipo_conta = Mid(aux, Len(tagOFX) + 1, Len(aux) - ((Len(tagOFX) * 2) + 1))

'End If

'pega data inicio

tagOFX = "<DTSTART>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

dataOFX = Mid(aux, Len(tagOFX) + 1, 4 + 2 + 2) 'data em formato AAAAMMDD

data_ini = Left(dataOFX, 4) & "/" & Mid(dataOFX, 5, 2) & "/" & Right(dataOFX, 2)

Sheets("Extrato").Cells(3, 2).Value = data_ini

End If

'pega data fim

tagOFX = "<DTEND>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then


dataOFX = Mid(aux, Len(tagOFX) + 1, 4 + 2 + 2) 'data em formato AAAAMMDD

data_fim = Left(dataOFX, 4) & "/" & Mid(dataOFX, 5, 2) & "/" & Right(dataOFX, 2)

Sheets("Extrato").Cells(4, 2).Value = data_fim

End If

'pega o saldo final <BALAMT>

tagOFX = "<BALAMT>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

valor_saldo_final = Mid(aux, Len(tagOFX) + 1, Len(aux) - ((Len(tagOFX) * 2) + 1))

vlrSdFinal = Val(valor_saldo_final)

End If

'começa a rodar as transações

'tipo de transação

tagOFX = "<TRNTYPE>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

tipo_transacao = Mid(aux, Len(tagOFX) + 1, Len(aux) - ((Len(tagOFX) * 2) + 1))

Sheets("Extrato").Cells(cont, 2).Value = tipo_transacao

End If

'data da transacao

tagOFX = "<DTPOSTED>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

dataOFX = Mid(aux, Len(tagOFX) + 1, 4 + 2 + 2) 'data em formato AAAAMMDD

data_transacao = Left(dataOFX, 4) & "/" & Mid(dataOFX, 5, 2) & "/" & Right(dataOFX, 2)

Sheets("Extrato").Cells(cont, 1).Value = data_transacao

End If

'valor da transacao

tagOFX = "<TRNAMT>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

valor_transacao = Mid(aux, Len(tagOFX) + 1, Len(aux) - ((Len(tagOFX) * 2) + 1))


vlr = Val(valor_transacao)

Sheets("Extrato").Cells(cont, 7).Value = vlr

End If

'referencia da transacao <REFNUM>

tagOFX = "<REFNUM>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

ref_transacao = "'" & Mid(aux, Len(tagOFX) + 1, Len(aux) - ((Len(tagOFX) * 2) + 1))

Sheets("Extrato").Cells(cont, 3).Value = ref_transacao

End If

'memo da transacao <MEMO>

tagOFX = "<MEMO>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

memo_transacao = Mid(aux, Len(tagOFX) + 1, Len(aux) - ((Len(tagOFX) * 2) + 1))

Sheets("Extrato").Cells(cont, 4).Value = memo_transacao

End If

'adiciona linha ao EXTRATO </STMTTRN>

tagOFX = "</STMTTRN>"

If Mid(aux, 1, Len(tagOFX)) = tagOFX Then

cont = cont + 1

End If

i=i+1

aux = Sheets("AUX").Cells(i, 1).Value

Loop

Sheets("Extrato").Cells(cont - 1, 6).Value = vlrSdFinal

Sheets("Extrato").Cells(4, 7).Value = vlrSdFinal


Range("G3").Select

ActiveCell.FormulaR1C1 = "=R[4]C[-1]-R[4]C"

Sheets("Extrato").Select

'define primeira fórmula de saldo

'varre pra calcular o restate das fórmulas de saldo

For x = cont - 2 To 7 Step -1

Cells(x, 6).Select

ActiveCell.FormulaR1C1 = "=R[1]C-R[1]C[1]"

Next x

Calculate

'Sheets("Extrato").Cells(3, 7).Value = Sheets("Extrato").Cells(cont - 1, 6).Value

End Sub

Sub limpa_extrato()

Rows("7:650").Select

Selection.ClearContents

Range("A7").Select

Sheets("Extrato").Cells(3, 2).Value = ""

Sheets("Extrato").Cells(4, 2).Value = ""

Sheets("Extrato").Cells(2, 4).Value = ""

Sheets("Extrato").Cells(3, 4).Value = ""

Sheets("Extrato").Cells(4, 4).Value = ""

Sheets("Extrato").Cells(3, 7).Value = ""

Sheets("Extrato").Cells(4, 7).Value = ""

End Sub

Sub Macro5()

'
' Macro5 Macro

'

'

Range("F7").Select

ActiveCell.FormulaR1C1 = "=R[-4]C[1]+RC[1]"

Range("F8").Select

ActiveCell.FormulaR1C1 = "=R[-1]C+RC[1]"

Range("F9").Select

End Sub

Sub Macro6()

'

' Macro6 Macro

'

'

End Sub

Sub Macro7()

'

' Macro7 Macro

'

'

Range("G4").Select

End Sub

Вам также может понравиться