-1

I would like to know if there is any VBA code which allows me to execute .sql files by clicking a button in Excel?

I've been surfing different webs, but all of them showed code which consists into write the SQL query in the macro, despite it isn't what I wanted. So I have stored .sql scripts as files in different folders on my computer and what I want is to run this script by clicking a button in Excel using VBA.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Short answer is no. There is no magic button in Excel that does that. Easiest that I can think of is run/import that .sql to MySQL, SQL Server, Postgres, etc. to become a database. Example using MS SQL Server:

How do I import a .sql data file into SQL Server?

Once you run successfully that .sql into a DBMS, you may use built-in tools of that DBMS to export the tables into Excel. See the link below as an example:

How to Import and Export SQL Server data to an Excel file

It may not be as easy as pressing a button but not as tedious as hardcoding it. I hope it helps.

Wils Mils
  • 613
  • 4
  • 9
  • Hey, thanks a lot for your answer! Indeed, I have created these scripts in SQL Server and as I have to execute some codes in VBA which update this databases. I wanted to find this code to open those .sql files and run them. I know you have for instance the VBA code "Do.Cmd.OpenQuery" to open Access db and run any query save in this Access db. – ignacio diaz Jul 14 '20 at 08:07