I thought I was done with this but I ended up getting a type mismatch error when I added the customer to the WHERE statement. I can swap out the Item and the Cust filters without any problem but when I have them both included I get the mismatch error.
Thank you in advance for anyone who can point me in the right direction. I am afraid I will have to add a bit of complexity to the WHERE clause to resolve?
Code:
Set sDate = Sheets("Sheet1").Range("Start_Date")
Set EDate = Sheets("Sheet1").Range("End_Date")
Set Item = Sheets("Sheet1").Range("Items")
Set Cust = Sheets("Sheet1").Range("Customers")
'
With ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, Version:= _
xlPivotTableVersion12)
.Connection = Array(Array( _
"ODBC;DSN=Access;DBQ=C:\Commissions\2011 POS\POS 2011.mdb;DefaultDir=C:\Commissions\2011 POS;DriverId=25;FIL=MS Access;MaxBufferSize=" _
), Array("2048;PageTimeout=5;"))
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT YTD_POS.`INDEX NUMBER`, YTD_POS.`POS PARTNER`, YTD_POS.DATE, YTD_POS.ITEM, YTD_POS.QTY, YTD_POS.PRICE, YTD_POS.REVENUE, YTD_POS.CUSTOMER, YTD_POS.CUSTOMER_VARIATION, YTD_POS.SALESREP, YTD_POS.W" _
, _
"EEK, YTD_POS.MONTH, YTD_POS.MONTH_CODE, YTD_POS.QUARTER, YTD_POS.YEAR, YTD_POS.INSERT_DATETIME, YTD_POS.NEW_CAT, YTD_POS.TERRITORY, YTD_POS.ZIP_CODE, YTD_POS.`SEGMENTATION 2`, YTD_POS.SEGMENTATION, YT" _
, _
"D_POS.`LINE OF BUSINESS`, YTD_POS.`OM CATEGORY NAME`, YTD_POS.STATE, YTD_POS.COUNTRY, YTD_POS.`SHIP TO CUSTOMER`, YTD_POS.`SHIP TO STATE`, YTD_POS.`SHIP TO COUNTRY`, YTD_POS.`INVOICE NUMBER`" & Chr(13) & "" & Chr(10) & "FROM `C:" _
, _
"\Commissions\2011 POS\POS 2011.mdb`.YTD_POS YTD_POS" & Chr(13) & "" & Chr(10) & "WHERE (YTD_POS.DATE>=#" & sDate & "# And YTD_POS.DATE<=#" & EDate & "#) AND (YTD_POS.ITEM IN(" & Item & ")) AND (YTD_POS.CUSTOMER_VARIATION IN(" & Cust & "))" _
)
.CreatePivotTable TableDestination:="'[POS 2011 Test.xlsx]Sheet1'!R3C1", _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion12
Thank you in advance for anyone who can point me in the right direction. I am afraid I will have to add a bit of complexity to the WHERE clause to resolve?