macro to load SELECTED query to worksheet

ooptennoort

Board Regular
Joined
Mar 29, 2021
Messages
67
Office Version
  1. 365
Platform
  1. Windows
I have this generic VBA code that allows me to load a query to a worksheet (by default I don't load queries to save time etc.) but it only works if I fill in the query name.
But I want to be able to simply select the query and then run my macro (which will then fill in selected query's name).
Anyone?

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & query.Name _
, Destination:=Range("$A$1")).QueryTable

I am sort of looking for the macro equivalent of Active.Query or selected.query (which I don't believe exist)... workaround???
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You won't be able to run code at any point where you would be able to select a query. You'd have to either just prompt for the query name or create a userform (for example) with a list of queries in it.
 
Upvote 0
You won't be able to run code at any point where you would be able to select a query. You'd have to either just prompt for the query name or create a userform (for example) with a list of queries in it.
Mmmh... you mean first load a list of queries and then have the macro get values (i.e.) from selected cells... 1 name I can understand with dim (or was it VAR?) but how do I get more than 1 names into that code above (I guess, replacing query.Name (without quotes or anything??)?
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,161
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top