I have Access data (one column) and I want to put these data in a specified field in Excel in a SPECIFIED sheet.<br><br>I know how to export from Access to Excel but how do I choose my worksheet?
Let's say you have a spreadsheet called "StateStats.xls"<br>On there you have 4 sheets (Seattle, Pittsburgh, Dallas, Miami)<br>In Cell B5 of the "Dallas" Sheet you want to enter data, and continue down from there.<br>I put the following code on Click of a button called cmdExport, you can put it where you want.<br><br>There are comments in between some stuff. Replace the bolded areas with your own info:<br><br>-----<br><br>Private Sub cmdExport_Click()<br> Dim rs As Recordset<br> Dim strSQL As String, strSpreadsheet As String<br> Dim objXLT As Object<br> Dim objWorksheet As Object<br> Dim x As Integer<br> <br>On Error GoTo Export_Err<br> DoCmd.Hourglass True<br> ' Create the string for the recordset<br> strSQL = <b>"SELECT TestDate FROM tblTest WHERE (((Status)= 'Active'));"</b><br> <br> strSpreadsheet = <b>"C:\My Documents\Spreadsheets\StateStats.xls"</b><br> <br> Set db = CurrentDb()<br> Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)<br> <br> ' If there are records, then<br> With rs<br> If .RecordCount > 0 Then<br> ' Open the template<br> Set objXLT = GetObject(strSpreadsheet)<br> ' Make it visible<br> objXLT.Application.Visible = True<br> objXLT.Parent.Windows(1).Visible = True<br> <br> Set objWorksheet = objXLT.worksheets(<b>"Dallas"</b>)<br> objWorksheet.Activate<br> <br> 'Insert the data from the recordset into the appropriated cells on the template<br> x = 5<br> Do Until .EOF ' The first number (x) is the row, the second number is the column<br> objXLT.activesheet.cells(x, 2).Value = !<b>testdate</b><br> 'objXLT.activesheet.cells(x, 3).Value = !BegSeqNo ' If you had more than one field<br> x = x + 1<br> rs.MoveNext<br> Loop<br> End If<br> End With<br> <br>Export_Exit:<br> rs.CLOSE<br> Set objXLT = Nothing<br> Set objWorksheet = Nothing<br> db.CLOSE<br> DoCmd.Hourglass False<br> Exit Sub<br> <br>Export_Err:<br> MsgBox Err.Number & ": " & Err.Description<br> Resume Export_Exit<br>End Sub<br><br>-----<br><br>This will work, have done it many times. Let me know if you have trouble. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href=
I think I can use this concept for a project I'm working on, but I have a couple of questions.
1. Can a Workbook be created "on the fly" from within the VB code and can the individual worksheets be renamed at that same time? Or, does the Workbook have to already exist in order to be referenced?
2. The data I need to insert into the worksheets will come from a series of cross tab queries where the number of columns can vary from month to month. Is it possible to capture the number of returned columns (similar to .RecordCount)?
3. Is there an alternative to this process? What I'm thinking of would be similar to pasting a Pivot Table into an existing worksheet where only the beginning cell reference (i.e., J21) needs to be supplied.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.