try doing it all in code using ADODB.
First, on each sheet, create a new named range in cell A1 (maybe) called:
"Query" & xsheet.name
in that cell put the query specific to that sheet
then use this code in a VBA Code module:
sub getData()
dim cn as new adodb.connection
dim rs as new adodb.recordset
dim connStr as string ' connection string
dim sUDLFile as string ' path and name of Microsoft Data Link File (UDL FILE)
dim xSheet as worksheet
connStr="File Name=" & sUDLFile
cn.open connstr
'loop through all the worksheets
for each xSheet in thisworkbook.worksheets
with rs
' open the connection to the db...
.activeconnection=cn
'get the query from the range on the worksheet!
sQry=xsheet.range("Query" & xsheet.name).text
' open the query from the DB
.open sQry
' dump the dataset onto the worksheet with one line of code in B5 cell!
xsheet.range(B5).copyfromrecordset rs
.close
end with
next
' clean up and release memory
cn.close
set cn=nothing
set rs=nothing
'
end sub
to create your Connection string, (UDL FILE) in MS Windows Explorer:
- navigate to the directory where your workbook is
- right-click and choose New...>Microsoft Data Link.
- change the name to a good one (name.udl maybe)
- double click the new file and set the settings to create and test a connection to the db
any problems, just ask!
Philip