Sub FormatExcel_Recordset(Filename As String, NewFile As Boolean, Optional Customize As String)
'For a new file, filename is the name of the file without path or extension
'NewFile is True/False. If New File set to True, If an existing file, set to false
'''''''''''''''''''''''''''''''''''''''''''
'Test changes to using qdf to assign date where clause
'''''''''''''''''''''''''''''''''''''''''''
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset
Dim stCustomize As String
Dim FName As Variant
Dim FNameExists As Boolean
Dim yesno
Set objapp = CreateObject("Excel.Application")
objapp.Visible = True
If NewFile Then
Set wb = objapp.workbooks.Add
Else
Set wb = objapp.workbooks.Open(Filename, True, False)
End If
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblHeader")
Set rs1 = CurrentDb.OpenRecordset("SELECT * FROM tblDetail")
For Each ws In wb.Worksheets
'Debug.Print ws.Name
With ws
.Activate
rs1.MoveLast
rs1.MoveFirst
'Paste Header row
.Cells(1, 1).CopyFromRecordset rs
'Paste Detail
.Cells(3, 1).CopyFromRecordset rs1
' lastrow = rs1.RecordCount + 1
' lastCol = .Range("A1").CurrentRegion.Columns.Count
'
' Select Case stCustomize
' Case "EvalCourse"
' .Range("A2:AD2").Copy
' .Range("A2:AD" & lastrow + 1).PasteSpecial xlPasteFormats
' .Range("AH2:AS2").Copy
' .Range("AH2:AS" & lastrow + 1).PasteSpecial xlPasteAll
' .Application.CutCopyMode = False
' .Range("AH" & lastrow + 1).Formula = "=SUM(AH2:AH" & lastrow & ")"
' .Range("AI" & lastrow + 1).Formula = "=SUM(AI2:AI" & lastrow & ")/$AH" & lastrow + 1
' .Range("AI" & lastrow + 1 & ":AR" & lastrow + 1).FillRight
' .Range("AE2:AF" & lastrow).Clear
' '*****************
' 'Add Total Row with avgs
' .Cells(lastrow + 1, 1) = "TOTALS"
' .Range("A" & lastrow + 1 & ":AS" & lastrow + 1).Font.Bold = True
' .Range("K" & lastrow + 1).Formula = "=AVERAGE(K2:K" & lastrow & ")"
' .Range("K" & lastrow + 1 & ":AD" & lastrow + 1).FillRight
' .Cells(lastrow + 1, 1).Interior.Color = RGB(255, 255, 0) 'Yellow
' .Range("B" & lastrow + 1 & ":P" & lastrow + 1).Interior.Color = RGB(73, 69, 41) 'Brown
' .Range("K" & lastrow + 1 & ":M" & lastrow + 1).Interior.Color = RGB(51, 153, 102) 'Green
' .Range("N2").Formula = "=AVERAGE(K2+L2+M2)/3"
' .Range("N2:N" & lastrow).FillDown 'Except Total Row
' .Range("N" & lastrow + 1).Interior.Color = RGB(255, 255, 0) 'Yellow
' .Range("Q" & lastrow + 1 & ":AC" & lastrow + 1).Interior.Color = RGB(51, 153, 102) 'Green
' .Range("S2").Formula = "=AVERAGE(Q2+R2)/2"
' .Range("S2:S" & lastrow).FillDown 'Except Total Row
' .Range("S" & lastrow + 1).Interior.Color = RGB(255, 255, 0) 'Yellow
' .Range("W2").Formula = "=AVERAGE(T2+U2+V2)/3"
' .Range("W2:W" & lastrow).FillDown 'Except Total Row
' .Range("W" & lastrow + 1).Interior.Color = RGB(255, 255, 0) 'Yellow
' .Range("AD2").Formula = "=AVERAGE(X2+Y2+Z2+AA2+AB2+AC2)/6"
' .Range("W2:W" & lastrow).FillDown 'Except Total Row
' .Range("AD" & lastrow + 1).Interior.Color = RGB(255, 255, 0) 'Yellow
' .Range("AL" & lastrow + 1).Interior.Color = RGB(242, 220, 219) 'Light Pink
'
' '*****************
' Case "InstMatAvg"
' .Range("A2:F2").Copy
' .Range("A2:F" & lastrow).PasteSpecial xlPasteFormats
' .Application.CutCopyMode = False
' .AutoFilterMode = False
' .Range("A1:H" & lastrow).AutoFilter Field:=8, Criteria1:="1"
' .Range("E2:F" & lastrow).Interior.Color = RGB(218, 150, 148)
' .Range("A1:H" & lastrow).AutoFilter Field:=8, Criteria1:=">1" 'More than one instructor
' .Range("E2:F" & lastrow).Interior.Color = RGB(177, 160, 199)
' .AutoFilterMode = False
' .Range("G2:H" & lastrow).Clear
' '*****************
' 'Add Total Row with avgs ?
' 'Add Footnote (Single/Dual Instructor color code)
' If InStr(ws.Name, "Avg") Then
' stCol = "A"
' Else
' stCol = "B"
' End If
' .Range(stCol & lastrow + 3) = "Single Instructor Instances"
' .Range(stCol & lastrow + 4) = "Dual Instructor Instances"
' .Range(stCol & lastrow + 3).Interior.Color = RGB(218, 150, 148) 'Pink
' .Range(stCol & lastrow + 4).Interior.Color = RGB(177, 160, 199) 'Purple
' .Range(stCol & lastrow + 3 & ":A" & lastrow + 4).Font.Size = 11
' .Range(stCol & lastrow + 3 & ":A" & lastrow + 4).Font.Bold = True
' .Range(stCol & lastrow + 3 & ":A" & lastrow + 4).WrapText = True
' '*****************
'
' End Select
.Range("A1").Select
End With
Next
exit_sub:
wb.Worksheets(1).Activate
'prepopulate file name so user knows whether this
'is a course or conference spreadsheet. Name can
'be modified to another name as appropriate
'20160405
If Customize = "Course" Then
Customize = "Course Results"
ElseIf Customize = "AvgEvals" Then
Customize = "Avg Results"
ElseIf NewFile = False Then
Customize = "New File Name"
Else
Customize = Filename
End If
FNameExists = False
fnameSave:
Do While FNameExists = False
FName = wb.Application.GetSaveAsFilename(InitialFileName:=Customize, filefilter:= _
" Excel Macro Free Workbook (*.xlsx), *.xlsx", _
FilterIndex:=2, title:="Save to a new workbook")
fnameReplace:
If Dir(FName) = "" Then
wb.SaveAs FName, FileFormat:=51
FNameExists = True
ElseIf FName = False Then
MsgBox "File will not be saved", vbOKOnly + vbInformation, "Cancel SaveAs"
wb.Close savechanges:=False
FNameExists = True
Else
objapp.Visible = False
yesno = MsgBox("File " & FName & " already exists. Would you like to REPLACE this file? " & vbCrLf & vbCrLf & "Press No to choose another name; Cancel to quit without saving.", vbYesNoCancel, "File Exists")
objapp.Visible = True
If yesno = vbCancel Then
wb.Close savechanges:=False
FNameExists = True
ElseIf yesno = vbYes Then
Kill FName
GoTo fnameReplace
Else
GoTo fnameSave
End If
End If
Loop
rs.Close
rs1.Close
Set rs = Nothing
Set rs1 = Nothing
Set db = Nothing
objapp.Quit
Set objapp = Nothing
End Sub