Hi,
I wanted to post and share this solution that Stephen Cooper helped me get through. Estimated time savings per week is about 1.5hrs. Thank you Stephen
The Process: Each week I have multiple enrollment excel files that I get from our source data and are placed into a folder. Each file contains one group or Company. Each group can have members enrolling in five different products, I need to send these enrollments to another company in a particular format and particular mapping. An Access Db Picks up each of those excel files and maps the data and places each product type into its own table. Ther tables are now correctly mapped. The Compnay I send these enrollments to wants one enrollment file for each each group by product in excel, and this is what the code does for just one of those products:
Private Sub New_Accident_Click()
'
'Author: Stephen Cooper
'Email: @consultant.com
'Ph:
'In parameters
'Output
'Description: Will loop a query and export to multiple files
On Error GoTo HandleError:
Dim intMouseType As Integer
Dim strErrorMsg As String
Dim varReturn As Variant
Dim strFolderName As String
Dim strFileName As String
Dim strOutputPath As String
Dim strTemplateName As String
Dim strTemplatePath As String
Dim objFSO As Object
Dim strCompanyName As String
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim intStartRow As Integer
Dim strSQLData As String
Dim objXLApp As Object
Dim objXLBook As Object
Dim objXLMainSheet As Object
Dim rstData As DAO.Recordset
intMouseType = Screen.MousePointer
DoCmd.Hourglass True
Set db = CurrentDb
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Inserts the data beginning at row
intStartRow = 12
strTemplatePath = "R:\Admin Services\"
strTemplateName = "RA_Accident.xlsm"
strOutputPath = "R:\Admin Services\Reporting\"
strSQL = "select distinct [GroupName] from RA_Accident_NewEnrollment"
Set rst = db.OpenRecordset(strSQL)
'Now need to get a copy of the file
Do While Not rst.EOF
'strCompanyName = rst.Fields("GroupName").Value
strCompanyName = Replace(rst.Fields("GroupName").Value, ",", "")
strFileName = strOutputPath & "RA_NewEnrollment_Accident_" & strCompanyName & ".xlsm"
'Delete if it exists
If objFSO.FileExists(strOutputPath & "RA_NewEnrollment_Accident_" & strCompanyName & ".xlsm") Then
objFSO.deletefile strOutputPath & "RA_NewEnrollment_Accident_" & strCompanyName & ".xlsm"
End If
'Get new file
objFSO.copyfile strTemplatePath & strTemplateName, strOutputPath & "RA_NewEnrollment_Accident_" & strCompanyName & ".xlsm"
'Now get recordset
'swap these around - build SQL string before opening recordset
strSQLData = "select * from RA_Accident_NewEnrollment where Replace([GroupName],',','') = '" & strCompanyName & "'"
Set rstData = db.OpenRecordset(strSQLData)
'rstData = db.OpenRecordset(strSQLData)
' rstData = db.OpenRecordset(strSQLData)
Set objXLApp = CreateObject("Excel.Application")
'Make it non visible. Speeds it up
objXLApp.Application.Visible = True
'Coop - 20/11/2014 - turn off events, so the file isnt looking for an ini file that isnt there yet.
objXLApp.EnableEvents = False
Set objXLBook = objXLApp.Workbooks.Open(strOutputPath & "RA_NewEnrollment_Accident_" & strCompanyName & ".xlsm", False, False)
Set objXLMainSheet = objXLBook.Worksheets("CI Advance_Acc Adv_LL")
'Set some values on the main sheet
With objXLMainSheet
.Cells(intStartRow + 1, 1).CopyFromRecordset rstData
'copies the data in cel bc and places it in C4
'Copies the data in bb13 and places it in c6 if the value in cell bc13 is not HealthAlliance
If .Range("bc13").Value <> "HealthAlliance" Then
.Range("C4").Value = .Range("bc13")
.Range("c6").Value = .Range("bb13")
.Range("BB13:BC500").Value = ""
.Range("A12").Select
End If
End With
objXLBook.Save
objXLBook.Close
rst.MoveNext
Loop
ExitHere:
On Error Resume Next
'Close all recordsets etc here
varReturn = SysCmd(acSysCmdClearStatus)
Screen.MousePointer = intMouseType
objXLApp.Close
Set objXLApp = Nothing
Set objXLBook = Nothing
rstData.Close
Set rstData = Nothing
Exit Sub
HandleError:
Resume ExitHere
MsgBox "Process Complete..."
End Sub