This forum has been moved to TeachExcel.com
Ask all future questions in the New Excel Forum.
ExcelKey
Sub AddWeeklyWorksheets2012()
Dim i As Integer
Dim ws As Worksheet
Dim strSheetName As String
'----- Add Jan Sheets -----'
Dim arrJan() As Variant
arrJan = Array(1, 8, 15, 22, 29)
For i = 0 To UBound(arrJan)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Jan " & arrJan(i)
Next i
'----- Add Feb Sheets -----'
Dim arrFeb() As Variant
arrFeb = Array(5, 12, 19, 26)
For i = 0 To UBound(arrFeb)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Feb " & arrFeb(i)
Next i
'----- Add Mar Sheets -----'
Dim arrMar() As Variant
arrMar = Array(4, 11, 18, 25)
For i = 0 To UBound(arrMar)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Mar " & arrMar(i)
Next i
'----- Add Apr Sheets -----'
Dim arrApr() As Variant
arrApr = Array(1, 8, 15, 22, 29)
For i = 0 To UBound(arrApr)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Apr " & arrApr(i)
Next i
'----- Add May Sheets -----'
Dim arrMay() As Variant
arrMay = Array(6, 13, 20, 27)
For i = 0 To UBound(arrMay)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "May " & arrMay(i)
Next i
'----- Add Jun Sheets -----'
Dim arrJun() As Variant
arrJun = Array(3, 10, 17, 24)
For i = 0 To UBound(arrJun)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Jun " & arrJun(i)
Next i
'----- Add Jul Sheets -----'
Dim arrJul() As Variant
arrJul = Array(1, 8, 15, 22, 29)
For i = 0 To UBound(arrJul)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Jul " & arrJul(i)
Next i
'----- Add Aug Sheets -----'
Dim arrAug() As Variant
arrAug = Array(5, 12, 19, 26)
For i = 0 To UBound(arrAug)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Aug " & arrAug(i)
Next i
'----- Add Sep Sheets -----'
Dim arrSep() As Variant
arrSep = Array(2, 9, 16, 23, 30)
For i = 0 To UBound(arrSep)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Sep " & arrSep(i)
Next i
'----- Add Oct Sheets -----'
Dim arrOct() As Variant
arrOct = Array(7, 14, 21, 28)
For i = 0 To UBound(arrOct)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Oct " & arrOct(i)
Next i
'----- Add Nov Sheets -----'
Dim arrNov() As Variant
arrNov = Array(4, 11, 18, 25)
For i = 0 To UBound(arrNov)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Nov " & arrNov(i)
Next i
'------ Add Dec Sheets -----'
Dim arrDec() As Variant, SheetName
arrDec = Array(2, 9, 16, 23, 30)
For i = 0 To UBound(arrDec)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Dec " & arrDec(i)
Next i
End Sub
Set ws = ThisWorkbook.Worksheets.Add
Set ws = ThisWorkbook.Worksheets.Add(Before:=Sheets(1))
'Module-wide vars
Dim i As Integer
Dim ws As Worksheet
Dim strSheetName As String
Dim tw As Workbook
Sub AddAllWeeklySheets2012()
'Click inside this macro and press F5 to add all
'sheets for the whole year.
AddJanSheets
AddFebSheets
AddMarSheets
AddAprSheets
AddMaySheets
AddJunSheets
AddJulSheets
AddAugSheets
AddSepSheets
AddOctSheets
AddNovSheets
AddDecSheets
End Sub
'***************************************************************
'Run procedures individually to add worksheets for a given month.
'Nothing will happen if you have run the macro above because of
'duplicate sheet names.
'***************************************************************
Sub AddJanSheets()
Dim arrJan() As Variant
'Store the dates of each Sunday in January 2012 in an array
arrJan = Array(29, 22, 15, 8, 1)
'Add a worksheet for each Sunday in the array
For i = 0 To UBound(arrJan)
strSheetName = "Jan " & arrJan(i)
'Check if the worksheet name already exists
Call Dup_Name(strSheetName)
'If yes, then don't add a sheet and exit the sub
If Dup_Name(strSheetName) = True Then Exit Sub
'Otherwise add a new sheet
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add
'Give the worksheet a name that corresponds to
'each January Sunday's date
ws.Name = strSheetName
'Repeat until the upper boundary of the array is reached
Next i
End Sub
Private Sub AddFebSheets()
Dim arrFeb() As Variant
arrFeb = Array(26, 19, 12, 5)
For i = 0 To UBound(arrFeb)
strSheetName = "Feb " & arrFeb(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("Jan 29"))
ws.Name = strSheetName
Next i
End Sub
Sub AddMarSheets()
Dim arrMar() As Variant
arrMar = Array(25, 18, 11, 4)
For i = 0 To UBound(arrMar)
strSheetName = "Mar " & arrMar(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("Feb 26"))
ws.Name = strSheetName
Next i
End Sub
Sub AddAprSheets()
Dim arrApr() As Variant
arrApr = Array(29, 22, 15, 8, 1)
For i = 0 To UBound(arrApr)
strSheetName = "Apr " & arrApr(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("Mar 25"))
ws.Name = strSheetName
Next i
End Sub
Sub AddMaySheets()
Dim arrMay() As Variant
arrMay = Array(27, 20, 13, 6)
For i = 0 To UBound(arrMay)
strSheetName = "May " & arrMay(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("Apr 29"))
ws.Name = strSheetName
Next i
End Sub
Sub AddJunSheets()
Dim arrJun() As Variant
arrJun = Array(24, 17, 10, 3)
For i = 0 To UBound(arrJun)
strSheetName = "Jun " & arrJun(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("May 27"))
ws.Name = strSheetName
Next i
End Sub
Sub AddJulSheets()
Dim arrJul() As Variant
arrJul = Array(29, 22, 15, 8, 1)
For i = 0 To UBound(arrJul)
strSheetName = "Jul " & arrJul(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("Jun 24"))
ws.Name = strSheetName
Next i
End Sub
Sub AddAugSheets()
Dim arrAug() As Variant
arrAug = Array(26, 19, 12, 5)
For i = 0 To UBound(arrAug)
strSheetName = "Aug " & arrAug(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("Jul 29"))
ws.Name = strSheetName
Next i
End Sub
Sub AddSepSheets()
Dim arrSep() As Variant
arrSep = Array(30, 23, 16, 9, 2)
For i = 0 To UBound(arrSep)
strSheetName = "Sep " & arrSep(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("Aug 26"))
ws.Name = strSheetName
Next i
End Sub
Sub AddOctSheets()
Dim arrOct() As Variant
arrOct = Array(28, 21, 14, 7)
For i = 0 To UBound(arrOct)
strSheetName = "Oct " & arrOct(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("Sep 30"))
ws.Name = strSheetName
Next i
End Sub
Sub AddNovSheets()
Dim arrNov() As Variant
arrNov = Array(25, 18, 11, 4)
For i = 0 To UBound(arrNov)
strSheetName = "Nov " & arrNov(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("Oct 28"))
ws.Name = strSheetName
Next i
End Sub
Sub AddDecSheets()
Dim arrDec() As Variant
arrDec = Array(30, 23, 16, 9, 2)
For i = 0 To UBound(arrDec)
strSheetName = "Dec " & arrDec(i)
Call Dup_Name(strSheetName)
If Dup_Name(strSheetName) = True Then Exit Sub
Set tw = ThisWorkbook
Set ws = tw.Worksheets.Add(after:=Sheets("Nov 25"))
ws.Name = strSheetName
Next i
End Sub
Private Function Dup_Name(ShtName As String) As Boolean
'Checks to see if a worksheet name already exists.
Dim Sht As Worksheet
'Loop through the sheets.
For Each Sht In ThisWorkbook.Worksheets
'Does the sheet name exist?
If Sht.Name = ShtName Then
'If yes, return True, do not add a sheet and exit the function.
Dup_Name = True
Exit Function
'If no, return False to the calling procedure, which will add a new sheet.
Else: Dup_Name = False
End If
'Repeat until all sheets have been ID'd.
Next Sht
End Function
Option Explicit
Const ThisYear As Long = 2012
Const StartDay As Long = vbSunday
Const LastSheetOnLeft As Boolean = True
Public Sub SetupSheets()
Dim Ws As Worksheet
Dim SheetDate As Date
SheetDate = DateSerial(ThisYear, 1, 1)
Do While Weekday(SheetDate) <> StartDay
SheetDate = SheetDate + 1
Loop
Do
If LastSheetOnLeft Then
Set Ws = Sheets.Add(Before:=Sheets(1))
Else
Set Ws = Sheets.Add(After:=Sheets(Sheets.Count))
End If
Ws.Name = UCase(Format(SheetDate, "mmm dd"))
SheetDate = SheetDate + 7
Loop While Year(SheetDate) = ThisYear
End Sub
Sub AddSundayWorksheets()
Dim i As Integer
Dim j As Integer
Dim userYear As Integer
Dim newMonth As Integer
Dim ws As Worksheet
Dim Sundays As Collection
Dim DVal As Date
Dim ShowYear As Boolean
Dim Months() As Variant 'Array that stores the months
'-Change the FOUR-DIGIT number to the desired year---------
userYear = 2012
'-----------------------------------------------------------
'-Change to True or False to append the year to sheet name--
ShowYear = False
'-------------------------------------------------------------
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "x"
Months = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
DVal = DateValue("Jan 1," & userYear) - 1
'Build a Collection of all Sunday dates for the given year
Set Sundays = New Collection
For j = 1 To 365
DVal = DVal + 1
If WeekDay(DVal) = 1 Then
Sundays.Add DVal
End If
Next j
'Add Sheets and name them
For i = 1 To Sundays.Count
newMonth = Month(Sundays(i))
If ShowYear = True Then
Set ws = ThisWorkbook.Worksheets.Add(before:=Worksheets("x"))
ws.Name = Months(newMonth - 1) & " " & Day(Sundays(i)) & " " & userYear
Else
Set ws = ThisWorkbook.Worksheets.Add(before:=Worksheets("x"))
ws.Name = Months(newMonth - 1) & " " & Day(Sundays(i)) & " "
End If
Next i
Application.DisplayAlerts = False
Worksheets("x").Delete
Application.DisplayAlerts = True
Worksheets(1).Select
Set Sundays = Nothing
End Sub
For i = 1 To Sundays.Count
newMonth = Month(Sundays(i))
Set ws = ThisWorkbook.Worksheets.Add(before:=Worksheets("x"))
ws.Name = Months(newMonth - 1) & " " & Day(Sundays(i)) & _
Iif(userYear, " " & userYear, "")
Next i
Return to Macros and VBA Questions
Users browsing this forum: Google Adsense [Bot] and 240 guests