Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7711827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:28:27+00:00 2026-06-01T01:28:27+00:00

With what VBA macro commands can I switch between sheets, in order to execute

  • 0

With what VBA macro commands can I switch between sheets, in order to execute specific Workbook Connecton SQL commands?

What I have tried is, to select a sheet, write the sheet specific sql command store that command in the specific connection’s SQL command and then refresh the workbook to get the result. The code executes only on the sheet that it originally was executed on, not the sheet I selected.

Sheets("Alex").Select

' The SQL command text
Dim comText As String
comText = ... // the SQL command text

' Changes the workbook's SQL command text to the text in the code above
With ActiveWorkbook.Connections("conCRM").ODBCConnection // conCRM - the name of the conection
    .commandText = comText
End With

' Refreshes data from the database
ActiveWorkbook.Connections("conCRM").Refresh

The thing that, in my opinion, is wrong is that the last line in the code targets the whole workbook and not the specific sheet I need.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:28:28+00:00Added an answer on June 1, 2026 at 1:28 am

    See if this is close to what you are trying to do. It takes 3 different SQL statements, adds a sheet for each one, executes it and puts each result on a new sheet.

    Sub addSQLResultsOnto3Sheets()
        Dim oConn As WorkbookConnection
        Dim oQt As QueryTable
        Dim oSh As Worksheet
        Dim i, SQL
    
        'Get the existing connection's connection string
        Set oConn = ActiveWorkbook.Connections("conCRM")
    
        'Three different SQL statements, to be output onto 3 different worksheets.
        SQL = Array("SELECT TOP 1 * FROM MyTable", "SELECT TOP 2 * FROM MyTable", "SELECT TOP 3 * FROM MyTable")
    
        For i = 0 To 2
            'Add a new sheet "Results N"
            Set oSh = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
            oSh.Name = "Results " & (i + 1)
    
            'Add a new sheet with SQL
            Set oQt = oSh.ListObjects.Add(xlSrcExternal, oConn.ODBCConnection.Connection, Destination:=oSh.Range("A1")).QueryTable
            oQt.CommandText = SQL(i)
            oQt.Refresh
        Next
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VBA macro that uses Microsoft MapPoint to calculate the distance between
I have an excel file with VBA macro that starts when the Workbook is
I recently inherited a VBA macro that needs to have validation logic added to
I have written a VBA macro in an excel spreadsheet that opens all workbooks
I have written an Excel VBA macro which imports data from a HTML file
I'm trying to write a vba macro for a group tha has one workbook
I need to invoke a VBA macro within an Excel workbook from a python
In an Excel formula you can use =ISERR(A1) or =ISERROR(A1) In a VBA macro
I have a VBA Word Macro that gets words from .txt list and color
Can I invoke an option on a COM Add-in from a VBA macro in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.