title: Is there a simple way to use Column Number in "WHERE" SQL statement
body: <p>I'm using SQL statement for the fist time in VBA, how can I use the column number of my Excel sheet in a SQL statement?</p>
  
  <p>Below is an example of what I want to do : </p>
  
  <pre class="lang-vb prettyprint-override"><code>"SELECT * FROM [Sheet1$] WHERE 'ColumnNumber4 =" &amp; mySQLVariable
  </code></pre>
  
  <p>As you see the only thing wrong there is 'ColumnNumber4'</p>
  
  <p>I've already tried this : </p>
  
  <pre class="lang-vb prettyprint-override"><code>"SELECT * FROM [Sheet1$] WHERE  [M] =" &amp; mySQLVariable
  </code></pre>
  
  <p>Where M is the name I have set for the Column Number 4 but this don't work as excepted : my .docx ask me to select a table but the table MUST be selected with the </p>
  
  <pre><code>"SELECT * FROM[Sheet1$] WHERE[M] = " &amp; mySQLVariable
  </code></pre>
  
  <p>NB : <code>SELECT * FROM[Sheet1$]</code> work :</p>
  
  <p>The excel file is like this  : <a href="https://i.stack.imgur.com/5YrWG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5YrWG.png" alt="Excel file"></a></p>
  
  <p>Before using the macro the docx looks like : </p>
  
  <p><a href="https://i.stack.imgur.com/ZVm7b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZVm7b.png" alt="Before Macro"></a></p>
  
  <p>and after it looks like this (Working as excepted)</p>
  
  <p><a href="https://i.stack.imgur.com/VkxPe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VkxPe.png" alt="After Macro"></a></p>
  
  <p>By deduction i think that <code>WHERE[M] = " &amp; mySQLVariable</code> is the problem</p>
  
  <p>In fact with WHERE I got :
  <a href="https://i.stack.imgur.com/otnhN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/otnhN.png" alt="With WHERE Statment"></a></p>
  
  <p>Here is my full code  : </p>
  
  <pre class="lang-vb prettyprint-override"><code>Sub Publipostage()
      'Nécessite d'activer la référence "Microsoft Word xx.x Object Library"
      Dim docWord As Word.Document
      Dim appWord As Word.Application
      Dim NomBase As String
      Dim i As Long
      Dim rs As Recordset
  
      i = 3
      mySQLVariable = "MyString"
      NomBase = "M:\User\Folder\FolderItem.xlsm"
  
      Application.ScreenUpdating = False
      Set appWord = New Word.Application
      appWord.Visible = True
      'Ouverture du document principal Word
      Set docWord = appWord.Documents.Open("M:\User\Folder\FolderItem.docx")
  
      MsgBox ActiveCell.Column
  
      Set rs = "SELECT * FROM [Interface-Test$]"
      Debug.Print rs(3).Name
  
  
      'fonctionnalité de publipostage pour le document spécifié
      With docWord.MailMerge
          'Ouvre la base de données
         .OpenDataSource Name:=NomBase, _ Connection:="Driver={Microsoft Excel Driver (*.xlsm)};" &amp; _ 
  "DBQ=" &amp; NomBase &amp; "; ReadOnly=True; ", _ 
  SQLStatement:="SELECT * FROM [Interface-Test$] WHERE [F4] = " &amp; mySQLVariable        
  
  'Spécifie la fusion vers l'imprimante
          .Destination = wdSendToNewDocument
          .suppressBlankLines = True
              'Prend en compte l'ensemble des enregistrements
              With .DataSource
                  .FirstRecord = wdDefaultFirstRecord
                  .LastRecord = wdDefaultLastRecord
              End With
          'Exécute l'opération de publipostage
          .Execute Pause:=False
      End With
  
      ' Activation du doucment principal de Publipostage et fermeture
      docWord.Activate
      docWord.Close savechanges:=False
      ' Affichage l'application Word
      appWord.Visible = True
      Set docWord = Nothing
      Set appWord = Nothing
  
      'Fermeture du document Word
      'docWord.Close False
      'appWord.Quit
  End Sub
  </code></pre>
  
  <p>Thanks for your time and consideration.</p>