title: Put VBA variable in SQL query in excel
body: <p>I have this code which works:</p>
  
  <pre><code>sqlQuery = ("SELECT monthdata.VAL, monthdata.MONTHVAL, monthdata.GREEN, monthdata.RED, monthdata.RAG, monthdata.CREATOR FROM data LEFT JOIN monthdata ON data.UID = monthdata.DATAUID WHERE [UID] = '" &amp; (IDcell) &amp; "' AND [MONTHVAL] = #" &amp; Format((month1), "mm/dd/yyyy") &amp; "#")
  </code></pre>
  
  <p>I'm specifically interested in the end, i.e this bit:</p>
  
  <pre><code>[MONTHVAL] = #" &amp; Format((month1), "mm/dd/yyyy") &amp; "#")
  </code></pre>
  
  <p>I now have a variable "month1formatted" which is already in mm/dd/yyyy format. So I tried to put this in as a replacement and lose the formatting bit. This is what I ended up with:</p>
  
  <pre><code>sqlQuery = ("SELECT monthdata.VAL, monthdata.MONTHVAL, monthdata.GREEN, monthdata.RED, monthdata.RAG, monthdata.CREATOR FROM data LEFT JOIN monthdata ON data.UID = monthdata.DATAUID WHERE [UID] = '" &amp; (IDcell) &amp; "' AND [MONTHVAL] = month1formatted"
  </code></pre>
  
  <p>However, strangley this doesn't work. Am I putting the variable in at the end in the wrong way?</p>
  
  <p>When I print the variable sqlQuery it is printing the text name of the variable <code>(i.e AND [MONTHVAL] = month1formatted)</code>, not the value of the variable but I know the variable is set correctly </p>