How to use ActiveSheet.Name in sql query

ServerDude

New Member
Joined
Mar 31, 2011
Messages
30
Hi Guys,

I have a script that is working well i am just trying to make it a little more efficent. The workbook is made of about 10 sheets. Each sheet has the name of business area. I am tring to set the criteria in the sql query to filter against this business area instead of filtering at the pivot table report.

The query exectutes, but instead of getting the work sheet name in the query i am getting 'ActiveSheet.Name'. When i use the ActiveSheet.Name in any other area of vba it is working.

What am i doing wrong? I have highlighted in red the area i am having difficulty with.

Sheets(Sheets(x).Name).Range("AB5").Select
Sheets(Sheets(x).Name).PivotTableWizard SourceType:=xlExternal, SourceData:=Array( _
"SELECT uthead.uh_account, uthead.uh_cashexp, uthead.uh_cash, uthead.uh_costprd, uthead.uh_cupchg, uthead.uh_date, uthead.uh_mref, uthead.uh_refund, uthead.uh_test, uthead.uh_visits, uthead.uh_oref, sn" _
, _
"ame.sn_name, uthead.uh_totstar, uthead.uh_totfin, umach.um_type" & Chr(13) & "" & Chr(10) & "FROM sname sname, umach umach, uthead uthead" & Chr(13) & "" & Chr(10) & "WHERE sname.sn_account = uthead.uh_account AND uthead.uh_mref = umach.um_ref AND ((uthead" _
, _
".uh_date>={d '" & Date1 & "'} And uthead.uh_date<={d '" & date2 & "'}) AND (uthead.uh_account<>'CYM001') AND (uthead.uh_oref=' ActiveSheet.Name '))" _
), Connection:=Array(Array( _
"ODBC;DSN=Vendman;UID=;;SourceDB=H:\Opera II V6\Data\Cymraeg\COMP_C.DBC;SourceType=DBC;Exclusive=No;BackgroundFetch=Yes;Collate=Machi" _
), Array("ne;Null=Yes;Deleted=Yes;"))

Any help appriciated.

p.s. i am using Excel 2003
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi

The following is a code fragment only (so you need to incorporate it within your code):

Rich (BB code):
AND (uthead.uh_account<>'CYM001') AND (uthead.uh_oref='" &  ActiveSheet.Name  & "'))" _
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,794
Members
449,468
Latest member
AGreen17

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