The vba code below is what I have come up with to take fields I want from a query I created in Access and paste the results into 10 separate Excel files(region 1-10). I have essentially just repasted this over four more times and changed DLSGP to my other 4 programs names that I am also adding to each regional workbook in excel. I have bolded below the two places where the program needs to be changed for each. I want to be able to simply have one function and rotate out the grant program once the loop has cycled through for each program. Any thoughts or ideas??
Thanks!
Option Compare Database
Option Explicit
'Addresses DLSGP Summary
Function SendToRegion1()
Dim rsOut As DAO.Recordset
Dim objExcel As Object
Dim objWB As Object
Dim objWS As Object
Dim strSQL As String
Dim i As Integer
Dim stRegions As String
Dim filename As String
i = 1
Do While i <= 10
Select Case i
Case 1
stRegions = "Region I"
filename = "Region_ I_PFSR_2011-03-15.xlsx"
Case 2
stRegions = "Region II"
filename = "Region_ II_PFSR_2011-03-15.xlsx"
Case 3
stRegions = "Region III"
filename = "Region_ III_PFSR_2011-03-15.xlsx"
Case 4
stRegions = "Region IV"
filename = "Region_ IV_PFSR_2011-03-15.xlsx"
Case 5
stRegions = "Region V"
filename = "Region_ V_PFSR_2011-03-15.xlsx"
Case 6
stRegions = "Region VI"
filename = "Region_ VI_PFSR_2011-03-15.xlsx"
Case 7
stRegions = "Region VII"
filename = "Region_ VII_PFSR_2011-03-15.xlsx"
Case 8
stRegions = "Region VIII"
filename = "Region_ VIII_PFSR_2011-03-15.xlsx"
Case 9
stRegions = "Region IX"
filename = "Region_ IX_PFSR_2011-03-15.xlsx"
Case 10
stRegions = "Region X"
filename = "Region_ X_PFSR_2011-03-15.xlsx"
End Select
Set objExcel = CreateObject("Excel.Application")
Set objWB = objExcel.Workbooks.Open _
("C:\Documents and Settings\carmes\Desktop\PFSR Region\" & filename)
Set objWS = objWB.Worksheets("DLSGP Summary")
strSQL = "Select [Fiscal Year/Program], [Amount Appropriated], [SumOfAllocation], [Current Obligated Amount], [Draw Downs], [Award Balance], [Current Holds], [Available Funds], [Number of Awards], [SumOfNumber of Awards with Holds], [Pct of Draw Downs], [Pct of Award Balance], [Pct of Current Holds], [Pct of Available Funds], [Pct of Award Balance on Hold], [Pct of Award Balance Available] From [REG - DLSGP2 - Summary] WHERE [Region] = '" & stRegions & "'"
Set rsOut = Application.CurrentDb.OpenRecordset(strSQL)
objWS.Range("A3").CopyFromRecordset rsOut
rsOut.Close
objWB.Save
objWB.Close
Set objWS = Nothing
Set objWB = Nothing
i = i + 1
objExcel.Quit
Set objExcel = Nothing
Loop
Set objWS = Nothing
Set objWB = Nothing
End Function
Thanks!
Option Compare Database
Option Explicit
'Addresses DLSGP Summary
Function SendToRegion1()
Dim rsOut As DAO.Recordset
Dim objExcel As Object
Dim objWB As Object
Dim objWS As Object
Dim strSQL As String
Dim i As Integer
Dim stRegions As String
Dim filename As String
i = 1
Do While i <= 10
Select Case i
Case 1
stRegions = "Region I"
filename = "Region_ I_PFSR_2011-03-15.xlsx"
Case 2
stRegions = "Region II"
filename = "Region_ II_PFSR_2011-03-15.xlsx"
Case 3
stRegions = "Region III"
filename = "Region_ III_PFSR_2011-03-15.xlsx"
Case 4
stRegions = "Region IV"
filename = "Region_ IV_PFSR_2011-03-15.xlsx"
Case 5
stRegions = "Region V"
filename = "Region_ V_PFSR_2011-03-15.xlsx"
Case 6
stRegions = "Region VI"
filename = "Region_ VI_PFSR_2011-03-15.xlsx"
Case 7
stRegions = "Region VII"
filename = "Region_ VII_PFSR_2011-03-15.xlsx"
Case 8
stRegions = "Region VIII"
filename = "Region_ VIII_PFSR_2011-03-15.xlsx"
Case 9
stRegions = "Region IX"
filename = "Region_ IX_PFSR_2011-03-15.xlsx"
Case 10
stRegions = "Region X"
filename = "Region_ X_PFSR_2011-03-15.xlsx"
End Select
Set objExcel = CreateObject("Excel.Application")
Set objWB = objExcel.Workbooks.Open _
("C:\Documents and Settings\carmes\Desktop\PFSR Region\" & filename)
Set objWS = objWB.Worksheets("DLSGP Summary")
strSQL = "Select [Fiscal Year/Program], [Amount Appropriated], [SumOfAllocation], [Current Obligated Amount], [Draw Downs], [Award Balance], [Current Holds], [Available Funds], [Number of Awards], [SumOfNumber of Awards with Holds], [Pct of Draw Downs], [Pct of Award Balance], [Pct of Current Holds], [Pct of Available Funds], [Pct of Award Balance on Hold], [Pct of Award Balance Available] From [REG - DLSGP2 - Summary] WHERE [Region] = '" & stRegions & "'"
Set rsOut = Application.CurrentDb.OpenRecordset(strSQL)
objWS.Range("A3").CopyFromRecordset rsOut
rsOut.Close
objWB.Save
objWB.Close
Set objWS = Nothing
Set objWB = Nothing
i = i + 1
objExcel.Quit
Set objExcel = Nothing
Loop
Set objWS = Nothing
Set objWB = Nothing
End Function