Is it possible to use conditional compilation for or use late binding for PowerQuery/WorkbookQuery Objects?

MoshiM

Active Member
Joined
Jan 31, 2018
Messages
440
Office Version
  1. 2016
Platform
  1. Windows
The WorkbookQuery became available with Excel 2016 and is available in the Microsoft Excel 16.0 Object Library. What conditional compilation or late-binding string will allow me to manage what code gets run.
I have no way of testing this, but I imagine earlier versions of Excel will just get compilation errors if I leave it as it is.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try
VBA Code:
Sub SafeQueryHandling()
    Dim wb As Workbook
    Dim queryObj As Object 'Do not use a reference to WorkbookQuery!
    Dim hasQueries As Boolean
    
    Set wb = ThisWorkbook
    
    On Error Resume Next
    Set queryObj = wb.Queries
    On Error GoTo 0
    
    If Not queryObj Is Nothing Then
        hasQueries = False
        Dim query As Object
        On Error Resume Next
        For Each query In queryObj
            Debug.Print "Name of request: " & query.Name
            Debug.Print "Formula (M-code): " & query.Formula
            hasQueries = True
        Next query
        On Error GoTo 0
        
        If Not hasQueries Then
            MsgBox "No queries in the workbook.", vbInformation
        End If
    Else
        MsgBox "The WorkbookQuery object is not available in this version of Excel.", vbExclamation
    End If
End Sub

Artik
 
Upvote 0
Solution

Forum statistics

Threads
1,226,112
Messages
6,189,041
Members
453,521
Latest member
Chris_Hed

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