VBA With Selection.ListObject.QueryTable as string?

mrbillk

New Member
Joined
May 29, 2013
Messages
2
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
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi and Welcome to the Board,

Sorry, but VBA expressions like that can't be built using strings

Try a conditional approach like this instead...
Code:
Dim QT As QueryTable


If [I] [your test for version is True][/I] Then
    Set QT = Selection.ListObject.QueryTable
Else
    Set QT = Selection.QueryTable
End If


With QT
    '....your code continues...
 
Upvote 0
:LOL: Thanks for the quick response,
That worked very well, and like I thought was very easy. Never thought about using Dim QT as QueryTable. With that I can pull a lot of code out of this routine.
Thanks again.
 
Upvote 0

Forum statistics

Threads
1,216,122
Messages
6,128,963
Members
449,480
Latest member
yesitisasport

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