Fix the CommandType = 0 Macro Error When Importing TXT Files in Excel
Question details
The user needs to fix an Excel macro that fails while importing a TXT file at the CommandType = 0 assignment.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A VBA macro imports a TXT file using a query table or external data connection.
- Observed behavior
- The macro may stop on the .CommandType = 0 line when the Excel version or connection type does not support that assignment.
Open the workbook in Excel and identify the TXT file and connection used by the macro. Enable macros only if you trust the workbook and its source.
Comment Out the Unsupported CommandType Assignment
Use this approach if the TXT import does not need an explicit command type.
In Excel, open Developer > Visual Basic, then find the module containing the TXT import macro.
In the code editor, add an apostrophe before the failing line so it reads '.CommandType = 0.
Run the macro from Developer > Macros and inspect the worksheet to confirm that the TXT data imports and refreshes as expected.

Review the Connection and Excel Version
Use this approach if the macro still fails or the connection requires a specific command type.
Work With Spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing spreadsheets. Its Excel format compatibility helps you move everyday workbook tasks to WPS, though it does not fix an Excel-specific VBA connection error.

Frequently Asked Questions
Why does CommandType = 0 cause an Excel macro error?
Some Excel versions or connection types do not support assigning zero to CommandType. Check the object and connection used by the macro before changing the property.
Can I delete the CommandType line from my TXT import macro?
You can comment it out in Developer > Visual Basic and test the macro. Keep it removed only if the TXT data imports and refreshes correctly without the assignment.
Where can I find the connection used by the macro?
Check the query table or connection references in the VBA code, and look under Data > Queries & Connections for the associated data source.
Should I replace zero with another CommandType value?
Only if the specific connection supports and requires that command type. Consult the documentation for the connection object and test the macro in the target Excel version.




