title: Excel VBA - Refresh selected queries / connections
body: <p><strong>Situation</strong></p>
  
  <p>I have 5 <code>Workbook connections</code> set up that query data from an MS Access database, and I have given them the names <code>qry_1</code>, <code>qry_2</code>, ... , <code>qry_5</code></p>
  
  <p>I have a selection of these query names listed in a table called <code>qry_Table</code> on a worksheet:</p>
  
  <pre><code>Query Name
  ----------
  qry_1
  qry_4
  qry_5
  </code></pre>
  
  <p>and I can loop through the table (using <code>.listObjects</code>) to get each query name as a <code>string</code></p>
  
  <p><strong>Question</strong></p>
  
  <p>I can loop through all the queries in the workbook and refresh them all, but I can't seem to work out how to select and refresh only those queries listed in the table, namely <code>qry_1</code>, <code>qry_4</code> and <code>qry_5</code>. </p>
  
  <p>I would like to be able to <code>set</code> the query object so that I can define the <code>.Connection</code> string too.</p>
  
  <p>Is this possible?</p>
  
  <p><strong>Notes</strong></p>
  
  <p>The code here shows how I get each row in the column <code>Query Name</code> of the table <code>qry_Table</code></p>
  
  <pre><code>    dim wksControl As worksheet
      dim objList As ListObject
  
      set wksControl = worksheets("Control")    'the worksheet that contains 'qry_Table'
      Set objList = wksControl.ListObjects("qry_Table")
  
      With objList.ListColumns("Query Name").DataBodyRange
  
          For i = 1 To .Rows.count              
              str = .Rows(i)    'query name to refresh
              '***Required: Define the qry to refresh
              Set qry = .QueryTable(str)  '&lt;~~ this code fails
              'code to .Refresh  BackgroundQuery:=False
  
          Next i
      End With
  </code></pre>
  
  <p>And to refresh all the queries in the workbook I can use</p>
  
  <pre><code>    'code for generic query connections
      For Each objList In wks.ListObjects
          If objList.SourceType = xlSrcQuery Then  'only refresh if it's the right query type
  
              'ensure it's using the right connection/database as specified on the Control sheet
              strConnection = "ODBC;DSN=MS Access Database;DBQ=" &amp; Range("dbFilePath") &amp; _
                  Range("dbName") &amp; ";DefaultDir=" &amp; Range("dbFilePath") &amp; _
                      ";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;"
  
              With objList.QueryTable
                  .Connection = strConnection
                  .BackgroundQuery = False
                  .Refresh
              End With
              count = count + 1
          End If
  
      Next
  </code></pre>