I have a button that creates an Excel document. I want it make it flow better and to adjust for leap year. It has to flow October through September and have the FY as the name of the tab your on.
My current code is just 1 sheet and very long, there has to be a way to make it more streamline.
My current code is just 1 sheet and very long, there has to be a way to make it more streamline.
Code:
Private Sub cmdT2T_Click()
Dim xlApp As Object
Dim xlWB As Object
Dim xlSh As Object
Dim rsRoster As DAO.Recordset
Dim rsLeave As DAO.Recordset
Dim dbCurr As DAO.Database
Dim strRoster As String
Dim strLeave As String
Dim rng As Object
'Set variables
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Add
Set xlSh = xlWB.ActiveSheet
xlApp.Visible = True
'Control Information
xlSh.Range("D:ND").ColumnWidth = "10"
xlSh.Cells(3, 1).Value = "DoD ID"
xlSh.Cells(3, 2).Value = "Last Name"
xlSh.Cells(3, 3).Value = "First Name"
xlSh.Columns("A").ColumnWidth = "10"
xlSh.Columns("B").ColumnWidth = "15"
xlSh.Columns("C").ColumnWidth = "15"
xlSh.Cells(2, 4).Value = "1"
xlSh.Cells(2, 4).HorizontalAlignment = xlVAlignCenter
xlSh.Cells(2, 5).Value = "2"
xlSh.Cells(2, 5).HorizontalAlignment = xlVAlignCenter
xlSh.Cells(3, 4).Value = "Friday"
xlSh.Cells(3, 4).HorizontalAlignment = xlVAlignCenter
With xlSh.Range("A1", "C2")
.MergeCells = True
.Interior.ColorIndex = 16
End With
With xlSh.Range("A3", "C3")
.HorizontalAlignment = xlVAlignCenter
.Font.Bold = True
.Font.Size = 14
End With
'Set the week days
xlSh.Range("D3").Autofill Destination:=xlSh.Range("D3:ND3"), Type:=xlFillWeekdays
'Set months title
With xlSh.Range("D1", "AH1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 37
.Value = "October"
End With
'Set day numbers for each month
xlSh.Range("D2:E2").Autofill Destination:=xlSh.Range("D2:AH2"), Type:=xlFillSeries
With xlSh.Range("AI1", "BL1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 33
.Value = "November"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("AI2:AK2")
xlSh.Range("AI2:AK2").Autofill Destination:=xlSh.Range("AI2:BL2"), Type:=xlFillDefault
With xlSh.Range("BM1", "CQ1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 37
.Value = "December"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("BM2:BO2")
xlSh.Range("BM2:BO2").Autofill Destination:=xlSh.Range("BM2:CQ2"), Type:=xlFillDefault
With xlSh.Range("CR1", "DV1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 33
.Value = "January"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("CR2:CT2")
xlSh.Range("CR2:CT2").Autofill Destination:=xlSh.Range("CR2:DV2"), Type:=xlFillDefault
With xlSh.Range("DW1", "EX1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 37
.Value = "February"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("DW2:DY2")
xlSh.Range("DW2:DY2").Autofill Destination:=xlSh.Range("DW2:EX2"), Type:=xlFillDefault
With xlSh.Range("EY1", "GC1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 33
.Value = "March"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("EY2:FA2")
xlSh.Range("EY2:FA2").Autofill Destination:=xlSh.Range("EY2:GC2"), Type:=xlFillDefault
With xlSh.Range("GD1", "HG1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 37
.Value = "April"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("GD2:GF2")
xlSh.Range("GD2:GF2").Autofill Destination:=xlSh.Range("GD2:HG2"), Type:=xlFillDefault
With xlSh.Range("HH1", "IL1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 33
.Value = "May"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("HH2:HJ2")
xlSh.Range("HH2:HJ2").Autofill Destination:=xlSh.Range("HH2:IL2"), Type:=xlFillDefault
With xlSh.Range("IM1", "JP1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 37
.Value = "June"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("IM2:IO2")
xlSh.Range("IM2:IO2").Autofill Destination:=xlSh.Range("IM2:JP2"), Type:=xlFillDefault
With xlSh.Range("JQ1", "KU1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 33
.Value = "July"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("JQ2:JS2")
xlSh.Range("JQ2:JS2").Autofill Destination:=xlSh.Range("JQ2:KU2"), Type:=xlFillDefault
With xlSh.Range("KV1", "LZ1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 37
.Value = "August"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("KV2:KX2")
xlSh.Range("KV2:KX2").Autofill Destination:=xlSh.Range("KV2:LZ2"), Type:=xlFillDefault
With xlSh.Range("MA1", "ND1")
.Font.Bold = True
.Font.Size = 12
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlVAlignCenter
.MergeCells = True
.Interior.ColorIndex = 33
.Value = "September"
End With
xlSh.Range("D2:F2").Copy Destination:=xlSh.Range("MA2:MC2")
xlSh.Range("MA2:MC2").Autofill Destination:=xlSh.Range("MA2:ND2"), Type:=xlFillDefault
'Shade in weekends
For Each rng In xlSh.Range("D3:ND3")
With rng
If rng.Value = "Saturday" Then
xlSh.Columns(.Column).Interior.ColorIndex = 16
ElseIf rng.Value = "Sunday" Then
xlSh.Columns(.Column).Interior.ColorIndex = 16
End If
End With
Next
'Put the list of soldiers
Set dbCurr = CurrentDb()
strRoster = "SELECT Roster.[DoD ID], Roster.[Last Name], Roster.[First Name] " _
& "FROM Roster " _
& "WHERE (((Roster.[Last Name]) Not Like 'AAA*') AND ((Roster.Status) Not Like 'Archive')) " _
& "ORDER BY Roster.[Last Name];"
Set rsRoster = dbCurr.OpenRecordset(strRoster)
xlSh.Cells(4, 1).CopyFromRecordset rsRoster
rsRoster.Close
Set rsRoster = Nothing
'Freeze the names
xlSh.Columns("D").Select
xlApp.ActiveWindow.FreezePanes = True