I've been getting better at VBA over the past couple years however I'd like to try and learn how to simplify/condense my code. The code below allows me to output a query "02_08_Overall_Latest_Week" to a specific tab and cell. The "Call Init_Paths" is just where I have my paths stored so I can use global vars Is there a better was of doing what I have been doing or is there a way I can condense my code below. I'd like to keep my "For Loop" in there as some queries I need the headings and some I do not want them. If there's a better way I'm open. Thanks!
Public Function Output_Overall()
Call Init_Paths
Dim cnn As ADODB.Connection
Dim MyRecordset As New ADODB.Recordset
Dim MySQL As String
Dim MySheetPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
Set cnn = CurrentProject.Connection
MyRecordset.ActiveConnection = cnn
MySQL = "SELECT * From "
MySQL = MySQL & "02_08_Overall_Latest_Week"
MyRecordset.Open MySQL
MySheetPath = Staging_Path
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
Xl.Visible = True
XlBook.Windows(1).Visible = True
Set XlSheet = XlBook.Worksheets("Overall")
For iCol = 1 To MyRecordset.Fields.Count
XlSheet.Cells(1, iCol).Value = MyRecordset.Fields(iCol - 1).Name
Next
XlSheet.Range("A2").CopyFromRecordset MyRecordset
MyRecordset.Close
Set cnn = Nothing
Set Xl = Nothing
XlBook.Save
XlBook.Close
Set XlBook = Nothing
Set XlSheet = Nothing
End Function