Reading an OFX file
-
- GoldLounger
- Posts: 2640
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Reading an OFX file
Can Excel import an OFX file using Power Query?
From what I have read, an XLM file can be imported and a table generated based on certain fields.
I know how to read the OFX file as a flat file and add to a worksheet various fields/records. I'm interested in a Power Query approach.
From what I have read, an XLM file can be imported and a table generated based on certain fields.
I know how to read the OFX file as a flat file and add to a worksheet various fields/records. I'm interested in a Power Query approach.
Regards,
John
John
-
- Administrator
- Posts: 79321
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- 2StarLounger
- Posts: 165
- Joined: 11 Jun 2012, 20:37
Re: Reading an OFX file
Could you attach 1, maybe 2, examples of such ofx files?
-
- 5StarLounger
- Posts: 611
- Joined: 14 Nov 2012, 16:06
Re: Reading an OFX file
See attachment:
To be opened in Excel, using VBA:
To be opened in Excel, using VBA:
Code: Select all
Workbooks.OpenXML "G:\OF\voorbeeld.ofx", , 2
You do not have the required permissions to view the files attached to this post.
-
- 2StarLounger
- Posts: 165
- Joined: 11 Jun 2012, 20:37
Re: Reading an OFX file
In the attached:
1. A query I put together very quickly called AFirstGo. It's on Sheet1.
2. A query lifted from https://community.fabric.microsoft.com/ ... m-p/355528 (HansV's link) with minor tweaks at the start and finish of the query. It's on Sheet2
3. Both the above queries use the named range filepathAndname which is at cell A1 of Sheet1. This will need adjusting by you to point to the file you want to query.
edit:I just noticed the ofx file sample was snb's, not the OP jsteven's, so I'd still like to see one or two of the OP's ofx files.
1. A query I put together very quickly called AFirstGo. It's on Sheet1.
2. A query lifted from https://community.fabric.microsoft.com/ ... m-p/355528 (HansV's link) with minor tweaks at the start and finish of the query. It's on Sheet2
3. Both the above queries use the named range filepathAndname which is at cell A1 of Sheet1. This will need adjusting by you to point to the file you want to query.
edit:I just noticed the ofx file sample was snb's, not the OP jsteven's, so I'd still like to see one or two of the OP's ofx files.
You do not have the required permissions to view the files attached to this post.
-
- GoldLounger
- Posts: 2640
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Re: Reading an OFX file
p45cal,
Please find attached a sample OFX file. I'll look at the other suggestions when I have an opportunity.
Please find attached a sample OFX file. I'll look at the other suggestions when I have an opportunity.
You do not have the required permissions to view the files attached to this post.
Regards,
John
John
-
- 2StarLounger
- Posts: 165
- Joined: 11 Jun 2012, 20:37
Re: Reading an OFX file
The Sheet2 query in my last attachment seems to work if you point cell A1 of Sheet1 at the sample file on your system which is not in the MACOSX folder (this file seems to have incorrect contents).
-
- GoldLounger
- Posts: 2640
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Re: Reading an OFX file
I was able to identify the file type of the Sample.OFX file. It was generated using a SGML (Standard Generalized Markup Language) format. Excel is unable to open the file with Power Query.
In order to resolve the challenge I converted the SGML file to a XML format with VBA. Power Query can process XML files.
The main difference with SGML and XML is the ending tag associated with each line.
Thanks to all who contributed.
In order to resolve the challenge I converted the SGML file to a XML format with VBA. Power Query can process XML files.
The main difference with SGML and XML is the ending tag associated with each line.
Code: Select all
Example: <TRNAMT>100.00 in SGML
<TRNAMT>100.00</TRNAMT> in XML
Regards,
John
John
-
- 5StarLounger
- Posts: 611
- Joined: 14 Nov 2012, 16:06
Re: Reading an OFX file
I used this macro to adapt the OFX-file to an XML-readable file:
Code: Select all
Sub M_snb()
With CreateObject("scripting.filesystemobject")
sn = Split(.opentextfile("G:\OF\sample.ofx").readall, vbCrLf)
For j = 0 To UBound(sn)
If Right(sn(j), 1) <> ">" Then sn(j) = sn(j) & Replace(Trim(Left(sn(j), InStr(sn(j), ">"))), "<", "</")
Next
.createtextfile("G:\OF\sample.ofx").write Join(sn, vbCrLf)
End With
End Sub
-
- GoldLounger
- Posts: 2640
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Re: Reading an OFX file
snb,
I modified your code relative to line feeds, ampersands and XML declarations.
Thank you for your suggestion.
I modified your code relative to line feeds, ampersands and XML declarations.
Code: Select all
Sub M_snb()
Dim sourceFile As String
Dim targetFile As String
Dim xmlDeclaration As String
Dim ofxStartTag As String
Dim ofxEndTag As String
Dim fileContent As String
Dim lines() As String
Dim j As Long
Dim contentWithTags As String
' Define the source and target files based on the parameters
sourceFile = Parameters.Range("C4").Value & Parameters.Range("C3").Value
targetFile = Parameters.Range("C6").Value
' Define the XML declaration and OFX tags
xmlDeclaration = "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCrLf & "<OFX>"
ofxEndTag = "</OFX>"
' Create a FileSystemObject
With CreateObject("scripting.filesystemobject")
' Read the entire content of the source file
fileContent = .opentextfile(sourceFile).readall
' Split the content by line
lines = Split(fileContent, vbLf)
' Iterate through each line to adjust tags and remove "[0:GMT]" and ampersands
For j = 0 To UBound(lines)
If Right(lines(j), 1) <> ">" Then
lines(j) = Replace(Replace(lines(j) & Replace(Trim(Left(lines(j), InStr(lines(j), ">"))), "<", "</"), "[0:GMT]", ""), "&", "&")
End If
Next
' Combine the lines back into a single string with vbLf as the separator
fileContent = Join(lines, vbLf)
' Prepend the XML declaration and OFX start tag, and append the OFX end tag
contentWithTags = xmlDeclaration & vbLf & fileContent & vbCrLf & ofxEndTag
' Write the modified content to the target file
.createtextfile(targetFile).write contentWithTags
End With
End Sub
Regards,
John
John
-
- 5StarLounger
- Posts: 611
- Joined: 14 Nov 2012, 16:06
Re: Reading an OFX file
The file you posted started with <OFX> and ended with </OFX)
Nevertheless:
Nevertheless:
Code: Select all
Sub M_snb()
' reading parameters in only 1 reading action
' no need for variables that contain invariable content
st = Parameters.Range("C3:C6")
With CreateObject("scripting.filesystemobject")
' replacing once instead of in every loop
' no need for an intermediate variable
sn = Split(Replace(Replace(.opentextfile(sn(1, 2) & sn(1, 1)).readall, "[0:GMT]", ""), "&", "&"), vbCrLf)
' Iterate through each line to add 'missing' endtags
For j = 0 To UBound(sn)
If Right(sn(j), 1) <> ">" Then sn(j) = sn(j) & Replace(Trim(Left(sn(j), InStr(sn(j), ">"))), "<", "</")
Next
' Write the modified content to the target file
.createtextfile(sn(1, 4)).write Join(Array("<?xml version=""1.0"" encoding=""UTF-8""?>", "<OFX>", Join(sn, vbCrLf), "</OFX>"), vbCrLf)
End With
End Sub
-
- GoldLounger
- Posts: 2640
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
-
- GoldLounger
- Posts: 2640
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Re: Reading an OFX file
sub,
I tried your latest post regarding the code and encounter a compile error: Sub or Function not defined.
It happens at "sn" in this line of code: Split(Replace(Replace(.opentextfile(sn
Your thoughts are appreciated.
I tried your latest post regarding the code and encounter a compile error: Sub or Function not defined.
It happens at "sn" in this line of code: Split(Replace(Replace(.opentextfile(sn
Your thoughts are appreciated.
Regards,
John
John
-
- Administrator
- Posts: 79321
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Reading an OFX file
I suspect that the line
should be
Code: Select all
st = Parameters.Range("C3:C6")
Code: Select all
sn = Parameters.Range("C3:C6")
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 611
- Joined: 14 Nov 2012, 16:06
Re: Reading an OFX file
No but referring to st should be adapted.
Code: Select all
Sub M_snb()
' reading parameters in only 1 reading action
' no need for variables that contain invariable content
st = Parameters.Range("C3:C6")
With CreateObject("scripting.filesystemobject")
' replacing once instead of in every loop
' no need for an intermediate variable
sn = Split(Replace(Replace(.opentextfile(st(1, 2) & st(1, 1)).readall, "[0:GMT]", ""), "&", "&"), vbCrLf)
' Iterate through each line to add 'missing' endtags
For j = 0 To UBound(sn)
If Right(sn(j), 1) <> ">" Then sn(j) = sn(j) & Replace(Trim(Left(sn(j), InStr(sn(j), ">"))), "<", "</")
Next
' Write the modified content to the target file
.createtextfile(st(1, 4)).write Join(Array("<?xml version=""1.0"" encoding=""UTF-8""?>", "<OFX>", Join(sn, vbCrLf), "</OFX>"), vbCrLf)
End With
End Sub
-
- GoldLounger
- Posts: 2640
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Re: Reading an OFX file
snb,
This line errors: Script out of range.
sn = Split(Replace(Replace(.opentextfile(st(1, 2) & st(1, 1)).readall, "[0:GMT]", ""), "&", "&"), vbCrLf)
This line errors: Script out of range.
sn = Split(Replace(Replace(.opentextfile(st(1, 2) & st(1, 1)).readall, "[0:GMT]", ""), "&", "&"), vbCrLf)
Regards,
John
John
-
- 5StarLounger
- Posts: 611
- Joined: 14 Nov 2012, 16:06
Re: Reading an OFX file
then test:
Code: Select all
Sub M_snb()
st = Parameters.Range("C3:C6")
msgbox st(1,2)
msgbox st(1,1)
msgbox st(1,4)
End Sub
-
- GoldLounger
- Posts: 2640
- Joined: 26 Jan 2010, 16:31
- Location: Southern California
Re: Reading an OFX file
snb,
I found the issue: st = Parameters.Range("C3:C6") is a single-dimensional array.
Changing the code resolved the error message.
I found the issue: st = Parameters.Range("C3:C6") is a single-dimensional array.
Changing the code resolved the error message.
Code: Select all
Sub M_snb()
Dim st As Variant
st = Parameters.Range("C3:C6")
MsgBox st(1, 1)
MsgBox st(2, 1)
MsgBox st(3, 1)
End Sub
Regards,
John
John
-
- 5StarLounger
- Posts: 611
- Joined: 14 Nov 2012, 16:06
Re: Reading an OFX file
But nevertheless a 2-dimensional one, Ubound(st)=4, ubound(st,2)=1