Need VBA syntax to import to a specific sheet a Microsoft Access VBA Recordset?
Need help with exporting recordset to correct worksheet using Access/ Excel VBA:
Please note the following code.
I'm trying to get the recordset on the first worksheet - Sheet1 and it does put the data in there
But I'm trying to skip over to Sheet 3 - and it won't go to the third sheet in the workbook - it instead still - STILL puts in in Sheet 1 over the previous recordset.
Private Sub bSendExcel_Click() Dim sFileName As String Dim objApp As Excel.Application Dim wb As Excel.Workbook Dim ws As Excel.Worksheet sFileName = txtPath 'CLOSE OUT OF ALL EXCEL APPLICATIONS 'if open - remind to save and close out Dim rs As DAO.Recordset Set objApp = CreateObject("Excel.Application") objApp.Visible = True Set wb = objApp.Workbooks.Open(sFileName, True, False) Set rs = CurrentDb.OpenRecordset("qMonthlyWorkloadbyAsset_Crosstab") Set ws = wb.Sheets(1) For Each ws In wb.Worksheets With ws .Activate .Cells(2, 1).CopyFromRecordset rs End With Next rs.Close Set rs = Nothing'this is the second recordset... that still puts in Sheet 1 - even though I specify wb.Sheets(3)' Set rs = CurrentDb.OpenRecordset("_02_Final Hours Worked by Customer")' Set ws = wb.Sheets(3)' For Each ws In wb.Worksheets' With ws' .Activate' .Cells(2, 1).CopyFromRecordset rs' End With' Next' wb.Save Set wb = Nothing objApp.Quit Set objApp = NothingEnd Sub
Private Sub bSendExcel_Click() Dim sFileName As String Dim objApp As Excel.Application Dim wb As Excel.Workbook Dim ws As Excel.Worksheet sFileName = txtPath 'CLOSE OUT OF ALL EXCEL APPLICATIONS 'if open - remind to save and close out Dim rs As DAO.Recordset Set objApp = CreateObject("Excel.Application") objApp.Visible = True Set wb = objApp.Workbooks.Open(sFileName, True, False) Set rs = CurrentDb.OpenRecordset("qMonthlyWorkloadbyAsset_Crosstab") Set ws = wb.Sheets(1) ws.Activate ws.Cells(2, 1).CopyFromRecordset rs rs.Close Set rs = Nothing Set rs = CurrentDb.OpenRecordset("_02_Final Hours Worked by Customer") Set ws = wb.Sheets(3) ws.Activate ws.Cells(2, 1).CopyFromRecordset rs wb.Save Set wb = Nothing objApp.Quit Set objApp = NothingEnd Sub
I'm not sure how to delete a post any more either.
You should be able to select one of your posts as the solution.
Look at the post with the code that works, and in the upper right corner of that post you should see some blue text that says "select my solution" (or something like that). After clicking on that, I believe you will see a button popup at the bottom of that post that allows you to select your post.
Open in new window