Skip to content
Advertisement

Sql string with OLEDB gives error 1004 in VBA

I would like to import a text file into excel filtering just what I want through a VBA macro. When I use LIKE operator in the sql string I get the error 1004. I have tried both * and % as wildcard and ALike instead of Like but there is no difference.

test_7.txt

and the macro is:

Advertisement

Answer

SQL statements do not work well with the Power Query provider. But you could totally achieve what you want via the formula associated with your query.

See this example:

You might want to add some error handling mechanisms (in case the file is already open etc) and to delete the connection to the text file if it’s not further needed.

You’ll find the documentation for Microsoft’s Power Query M formula language here.

A last tip to help you build you M formulas with VBA: use the macro recorder!

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement