I have issues with a VBA project in Excel that used to work before for over a year.
My code is below, the last line is what gives the error. What I am trying to do is querying a sheet on the same workbook. I have other queries that call other workbooks but this module that calls a sheet on the same workbook is whats giving troubles.
Some key information:
- The workbook is binary
- The
srcDataSheet
value is [Data$A1:Y68277]. Of course, the row number could change but columns will always be A to Y. - Sheet named Data does exist.
- Tried changing providers.
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _ & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
What could possibly be causing this error when the workbook worked perfectly for over a year? Please help.
Sub QueryDataSheet(srcDataSheet As String)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strFile As String, strCon As String, strSQL As String, strRangeAddress As String
Dim iCols As Long, lastRow As Long, fLastRow As Long
strFile = ThisWorkbook.FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "SELECT [PLANT], [FILTER_PLANT], [Rule], [SKU], [Style], [Color], [Size Code], " & _
"Sum(IIf([Order_Type2]='XDR', [Corrected Qty], 0)) AS XDR, Sum(IIf([Order_Type2]='XDR CONSUMED', [Corrected Qty], 0)) AS XDR_CONSUMED, " & _
"Sum(IIf([Order_Type2]='EXCESS', [Corrected Qty], 0)) AS EXCESS, [RULE_RANGE], [I Indicator] " & _
"FROM " & srcDataSheet & " " & _
"WHERE [PLANT] IN ('0F','4J') " & _
"GROUP BY [PLANT], [FILTER_PLANT], [Rule], [SKU], [Style], [Color], [Size Code], [RULE_RANGE], [I Indicator]"
rs.Open strSQL, cn 'ERROR HERE