How to Use the Origin Option When Importing Text Files in Excel VBA
Question details
The user needs to write a VBA macro to import text or CSV files into Excel, ensuring the correct encoding is applied using the Origin setting to prevent text garbling.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating the import of external text files (such as tab-delimited Macintosh files or UTF-8 CSVs) into Excel via a VBA macro with specific encoding requirements.
- Observed behavior
- When text files are imported without an explicit Origin setting, special characters or specific file encodings may not render correctly in the spreadsheet.
Verify the original character encoding of your text file (e.g., UTF-8, Windows, or Macintosh) to ensure you use the correct Origin argument value in your VBA script.
Use Workbooks.OpenText with the Appropriate Origin Value
The Workbooks.OpenText method provides precise control over delimiters and file encoding during the import process.
This is the most reliable method for importing text and CSV files via VBA because it allows you to explicitly define the text qualifiers, delimiters, and the exact character encoding mapping using the Origin parameter.
Press Alt + F11 in Excel to open the Visual Basic for Applications (VBA) editor. Click 'Insert' > 'Module' to create a new script area.
Define your macro and use the Workbooks.OpenText method. Assign your file path to the Filename argument and set your specific encoding to the Origin argument. For example, for Macintosh files, use Origin:=xlMacintosh. For UTF-8 files, use Origin:=65001.
Add parsing arguments to ensure your text splits into columns properly. Add DataType:=xlDelimited, and include Tab:=True or Comma:=True depending on your file's structure.
Use Workbooks.Open for Simple File Imports
If you do not need extensive delimiter parsing, you can use the simpler Workbooks.Open method while still supplying the Origin argument.
Use WPS Spreadsheet to Run VBA Macros and Import Text Files
WPS Spreadsheet offers robust built-in support for VBA macros, allowing you to seamlessly run your existing Excel scripts to import text files with custom Origin encodings without rewriting your code.
- 1. Open WPS Spreadsheet: Launch WPS Office and open a new Spreadsheet document.
- 2. Enable Developer Tools: Navigate to the 'Developer' tab on the top ribbon to access the macro and scripting tools.
- 3. Insert and Run Your Macro: Click on 'Visual Basic Editor' (or press Alt + F11), paste your Workbooks.OpenText script into a new module, and run it to easily import your encoded text file.

Frequently Asked Questions
What is the Origin value for UTF-8 encoding in Excel VBA?
The Origin value for standard UTF-8 encoding is 65001. You can apply this by passing the integer directly to the Origin parameter, like this: Origin:=65001.
Why is my imported CSV file showing strange characters via macro?
This generally happens when the imported file's encoding does not match your system's default encoding. Explicitly specifying the correct Origin parameter (such as xlMacintosh or 65001) in your Workbooks.OpenText macro will resolve character display issues.
What is the difference between Workbooks.Open and Workbooks.OpenText?
Workbooks.Open is a general method for opening standard spreadsheet files, while Workbooks.OpenText is specifically designed to handle raw text files. OpenText provides additional parameters to specify custom delimiters, column data types, and text qualifiers.




