Question : Populate from Access table to Excel workbook

Another company we interact with only accepts their media requests through a heavily formatted Excel workbook.  I've already written code to open the Excel file that's saved as the template and write values to it.

What I don't know how to do is to tell it write values from a table in Access to a range of cells because the data in the table fluctuates (from 1 to 20x request) and the workbook they have provided has columns to the left and right of said range that do not apply to the data.

I've attached my code.  The Access table I want to get the data from is called ABCCompany.   The sheet I need to write to is called "Statements" and the columns that I need to write between are C and I.   If necessary, you can make up the names of the fields/columns as I just need an example.

Thank you in advance for your assistance!
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
Option Compare Database
Option Explicit
 
Function PopulateABC()
Dim ExlApp As Object
Dim strFilename As String
Dim TodaysDate As String
Dim YourName As String
Dim YourEmailAddress As String
Dim SaveAsPath As String
 
'Defines the strings.
strFilename = "C:\ABCMediaRequest.xls"
TodaysDate = Format(Date, "mm/dd/yy")
YourName = Forms![ABC}![YourName]
YourEmailAddress = Forms![ABC]![YourEmailAddress]
SaveAsPath = "c:\Test123.xls"
 
'Opens Microsoft Excel.
Set ExlApp = CreateObject("Excel.Application")
'Opens the Master Excel workbook and makes it visible.
With ExlApp
        .Visible = True
        .Workbooks.Open (strFilename)
End With
With ExlApp
'Populates the Order Summary Sheet.
        .Sheets("Order Summary").Select
        ActiveSheet.Cells(6, "B").Value = TodaysDate
        ActiveSheet.Cells(7, "B").Value = YourName
        ActiveSheet.Cells(8, "B").Value = YourEmailAddress
 'Populates the Statements Sheet.
        .Sheets("Statements").Select
        ActiveSheet.Range("???").Value = Array("???")
End With
'Saves the Workbook as another file.
With ExlApp
        ActiveWorkbook.SaveAs (SaveAsPath)
End With
'Clean up.
Set ExlApp = Nothing
 
End Function
Open in New Window Select All

Answer : Populate from Access table to Excel workbook

You can use the CopyFromRecordset function to populate the spreadsheet:

Iy you have a recordset called rstExcel containing the data to be copied, and have ensured this matches the columns needed to populate in the spreadsheet - in this example, row 1 in the spreadsheet already contains the column headers, so I copy the data starting at Row 2, Column 1

  ActiveSheet.Cells(2, 1).CopyFromRecordset rstExcel


More details can be found here:

http://msdn.microsoft.com/en-us/library/aa165427(office.10).aspx
Random Solutions  
 
programming4us programming4us