title: How do I select a variable with a space in the name using VBA from a CSV file via ADO?
body: <pre><code>Sub GetTextFileData(strSQL As String, strFolder As String, rngTargetCell As Range)
  ' example: GetTextFileData "SELECT * FROM filename.txt", _
                "C:\FolderName", Range("A3")
  ' example: GetTextFileData "SELECT * FROM filename.txt WHERE fieldname = 'criteria'", _
                "C:\FolderName", Range("A3")
  Dim cn As ADODB.Connection, rs As ADODB.Recordset, f As Integer
      If rngTargetCell Is Nothing Then Exit Sub
      Set cn = New ADODB.Connection
      On Error Resume Next
      cn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" &amp; _
          "Dbq=" &amp; strFolder &amp; ";" &amp; _
          "Extensions=asc,csv,tab,txt;"
      On Error GoTo 0
      If cn.State &lt;&gt; adStateOpen Then Exit Sub
      Set rs = New ADODB.Recordset
      On Error Resume Next
      rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
      On Error GoTo 0
      If rs.State &lt;&gt; adStateOpen Then
          cn.Close
          Set cn = Nothing
          Exit Sub
      End If
      ' the field headings
      For f = 0 To rs.Fields.count - 1
          rngTargetCell.Offset(0, f).Formula = rs.Fields(f).name
      Next f
      rngTargetCell.Offset(1, 0).CopyFromRecordset rs ' works in Excel 2000 or later
      'RS2WS rs, rngTargetCell ' works in Excel 97 or earlier
      rs.Close
      Set rs = Nothing
      cn.Close
      Set cn = Nothing
  End Sub
  </code></pre>
  
  <p>I can't remember where I got the above code but it's really good. It allows me to use SQL on a CSV and extract the data I need.</p>
  
  <p>Now I have a csv file where the column names can have a space in it and have other symbols such as % in the name. Now the code doesn't work. How do I tell VBA to select a column with a name such as "Swing %" the code below didn't work</p>
  
  <pre><code>GetTextFileData "select Swing % as swing_pct from data.csv", "c:\somewhere", Range("A1")
  </code></pre>