title: Simplified VBA Code for External Data Source
body: <p>so in all my VBA macros that use SQL to query our database, I have just been using what is automatically entered when recording a macro in Excel. Is there a way to simplify it? It inserts it into a table first, but really I just want to the raw data in a spreadsheet... it doesn't need to look nice.</p>
  
  <pre><code>Sheets("Sheet1").Select
  With ActiveSheet.ListObjects.Add(SourceType:=0, Source:="ODBC;DSN=AS400;" _
      , Destination:=Range("$A$1")).QueryTable
      .CommandText = strSQL
      .RowNumbers = False
      .FillAdjacentFormulas = False
      .PreserveFormatting = True
      .RefreshOnFileOpen = False
      .BackgroundQuery = True
      .RefreshStyle = xlInsertDeleteCells
      .SavePassword = False
      .SaveData = True
      .AdjustColumnWidth = True
      .RefreshPeriod = 0
      .PreserveColumnInfo = True
      .ListObject.DisplayName = "Table_LOCAL_ITEM"
      .Refresh BackgroundQuery:=False
  End With
  ActiveSheet.ListObjects("Table_LOCAL_ITEM").Unlist
  Cells.Select
  Selection.ClearFormats
  </code></pre>