title: I'm looking to export specific access table fields to specific excel cells. My vba code isn't working
body: <p>Below is the code I'm currently am trying to use.  Basically I want to pick the fields from a table and export them to a specific cell range in excel.  When I run this code in Access I'm not getting any errors but I'm also not getting the 2nd excel temp file saved with the fields I selected in the cells I specified.  Thanks for any help.  Code below has been updated to reflect what is now working.  See below comments that helped resolve the issues I ran into.  </p>
  
  <pre><code>Option Explicit
  
  'Enter Location of your Template Here
  Const ExcelTemplate = "C:\Users\Desktop\Export Excel\Temp.xlsx"
  'Enter the Folder Directory to save results to
  Const SaveResutsFldr = "C:\Users\Desktop\Export Excel\"
  Sub CreateWorkbook()
  Dim SaveAsStr As String
  Dim ExcelApp, WB As Object
  Dim i As Integer
  Dim c As Integer
  
  c = DCount("*", "tbldata") + 4
  
  'Create Reference to Run Excel
  Set ExcelApp = CreateObject("Excel.Application")
  
  'Create Reference to your Table
  Dim T As Recordset
  Set T = CurrentDb.OpenRecordset("tblData")
  
  'Loop through all Record on Table
  If Not (T.BOF And T.EOF) Then
  T.MoveFirst
  End If
  Do While Not T.EOF
  'Open Your Excel Template
  Set WB = ExcelApp.Workbooks.Open(ExcelTemplate)
  
  For i = 5 To c
  'Enter your data from your table here to the required cells
  WB.Worksheets("sheet1").Range("B" &amp; i) = T("Field1")
  WB.Worksheets("sheet1").Range("C" &amp; i) = T("field2")
  WB.Worksheets("sheet1").Range("D" &amp; i) = T("field3")
  WB.Worksheets("sheet1").Range("E" &amp; i) = T("field4")
  'Repeat this line for each piece of data you need entered
  'Changing the Sheet name, cell range, a field name as per your requirements
  'WB.Wor...
  'WB.Wor...
  
  T.MoveNext
  Next i
  i = i + 1
  Loop
  'Save and Close the Workbook
  SaveAsStr = SaveResutsFldr &amp; ("Temp1") &amp; ".xlsx"
  WB.SaveAs SaveAsStr
  WB.Close
  Set WB = Nothing
  
  'Move to the Next Record
  
  
  'Close down the Excel Application
  ExcelApp.Quit
  Set ExcelApp = Nothing
  End Sub
  </code></pre>