I built an Excel workbook that uses Microsoft Query via VBA to pull data in from another Excel workbook. I pass different parameters to the VBA to perform a search for purchase order information. My problem is that this must run on various version of Excel (from 2000 to 2010) therefore I find the Excel version and set up separate WITH QueryTable statements to include or exclude the .ListObject.
One uses With Selection.ListObject.Query to include the .ListObject. (table in 2007 and later) but another one uses just Selection.Query. I can’t find a way to set the WITH statement as a String to either include the .ListObject. or exclude it based on the Excel version. When I tried setting a string variable to include or exclude it the query fails with “Object Required”. This should be simple, but I can’t seem to find a solution. What I tried is shown below. Appreciate any suggestions.
Dim LObject as String
LObject = ".ListObject."
With Selection & LOject & QueryTable ' need a way to build this statement
One uses With Selection.ListObject.Query to include the .ListObject. (table in 2007 and later) but another one uses just Selection.Query. I can’t find a way to set the WITH statement as a String to either include the .ListObject. or exclude it based on the Excel version. When I tried setting a string variable to include or exclude it the query fails with “Object Required”. This should be simple, but I can’t seem to find a solution. What I tried is shown below. Appreciate any suggestions.
Dim LObject as String
LObject = ".ListObject."
With Selection & LOject & QueryTable ' need a way to build this statement