Excel: how to import a text file with a macro part 1
Simple Excel interface with text files
This post describes the process of importing a text file and performing mapping (transposing or calculating a new value) using Excel. This can be extended, using other tools provided with the system you are interfacing with, to importing and posting of documents; for example posting of a journal, importing sales orders, posting fixed asset depreciation etc.
The idea is to remove the manual work and produce a consistent approach.
Part 1 – importing a file into Excel with a macro.
There is some competency assumed in Excel functions and an understanding of VBA to be able to extend this for your own purposes. I have provided a template sheet that can be amended as the framework; you can also record macros in Excel and put this together following the recipe. As with all recipes ingredients can be added and removed so the main purpose of this is to explain the idea and then parts can be used and modified as necessary.
Scenario:
You receive a delimited (csv) file from an external system, in this example Salesforce, and you would like to import the file into Excel. After importing the file you would like to create a double sided journal adding the GL accounts, references and analysis based on the data received.
Template sheet parts:
Excel sheets
- File data – sheet where the data will be loaded mapped and sorted.
- Journal – sheet written to create the journal.
VBA macro
- FileImport – linked to the import file button and is used to select and import a file into the file data sheet.
- Clean file – removes any line feed e.g. when used in some text input that is written to the file.
- FileConvert – writes the data from the file data sheet with summarisation and balancing lines (will be included in the second post).
Method – importing the file
Start by trying the sample file and template first before making any modification.
Your file structure
Identify the structure of the file, positions of the fields and the delimiter (comma, pipe, semicolon etc); this will be a case of knowing the field types and number of columns with data in the file so that Excel can parse the file. I would recommend preparing a table with the mapping to the corresponding Excel types you want to use. Excel types can be found online if you require other formats.
File specification
- File extension: CSV
- Delimiter: Semi Colon (;)
Macro modification
Using the template spreadsheet provided, amend the macro used to import the file into the worksheet. You will need to change types possibly or add new columns based on the structure of your file. In the module BBD and procedure (sub) “FileConvert” find the below code and align this with the table you created for your file layout.
Template sheet modification
In the “File data” sheet insert or remove columns before column K so that you retain the formulae columns that are highlighted and they don’t get overwritten when you import the file. Note don’t remove column A because this has a named range. The columns in the “File data” sheet should match with the structure of the file unless you have skipped any columns to import with the xlSkipColumn format.
Tip: Save the workbook as new versions before you test and make incremental changes. If something goes wrong you can always go back to a previously saved copy.
You are now ready to test your first changes.
Testing the changes
Click on the import file button and select your file. The file should import into the data sheet without overwriting the formulae columns.
You will notice that before the file is imported the data sheet is cleared. After the file imports the formulae are recalculated and copied down to the end of the data range.
The formulae allow you to provide additional fields for each row and might not provide results with your file data; you can always amend the base formulae in the heading row with a formula of your choice.
Conclusion
You have completed the first part of the changes to import a file into Excel. The next post will cover mapping the imported data and preparing a double sided journal entry.
The import template starter and sample file are available on request – contact us and we'll send them over.
BBD Consulting