VBA to automatically create A workbook with # of Sheets

thardin

Board Regular
Joined
Sep 29, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
I want to write a macro that will automatically create a Workbook with a specific number of sheets based on the amount of days in the current month(one worksheet for each day of the month).
With the worksheets named 1st, 2nd, 3rd, etc.

How would you do this?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I advice you not to use that many sheets, like 1 for 1 month. Keeping everything in one sheet is the best way. Then you can analyse from that one specific sheet
 
Upvote 0
How about
VBA Code:
Sub thardin()
   Dim Dys As Long, Curr As Long, i As Long
   
   Dys = Day(DateSerial(Year(Date), Month(Date) + 1, 0))
   With Application
      Curr = .SheetsInNewWorkbook
      .SheetsInNewWorkbook = Dys
      .Workbooks.Add
      .SheetsInNewWorkbook = Curr
   End With
   For i = 1 To Dys
      Sheets(i).Name = Ordinals(i)
   Next i
End Sub
Function Ordinals(ByVal Indx As Long) As String
   Dim i As Long
   Const Ord = "stndrdthththththth" ' 2 char suffixes
   i = Indx Mod 100
   If ((Abs(i) >= 10) And (Abs(i) <= 19)) Or ((Abs(i) Mod 10) = 0) Then
      Ordinals = i & "th"
   Else
      Ordinals = i & Mid(Ord, ((Abs(i) Mod 10) * 2) - 1, 2)
   End If
End Function
 
Upvote 0
Solution
How about
VBA Code:
Sub thardin()
   Dim Dys As Long, Curr As Long, i As Long
  
   Dys = Day(DateSerial(Year(Date), Month(Date) + 1, 0))
   With Application
      Curr = .SheetsInNewWorkbook
      .SheetsInNewWorkbook = Dys
      .Workbooks.Add
      .SheetsInNewWorkbook = Curr
   End With
   For i = 1 To Dys
      Sheets(i).Name = Ordinals(i)
   Next i
End Sub
Function Ordinals(ByVal Indx As Long) As String
   Dim i As Long
   Const Ord = "stndrdthththththth" ' 2 char suffixes
   i = Indx Mod 100
   If ((Abs(i) >= 10) And (Abs(i) <= 19)) Or ((Abs(i) Mod 10) = 0) Then
      Ordinals = i & "th"
   Else
      Ordinals = i & Mid(Ord, ((Abs(i) Mod 10) * 2) - 1, 2)
   End If
End Function
Thats PERFECT! almost like magic, lol.

Thank you!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Just one question though, how do you make it so this wb is saved AS SOON as it is created. because when I try this, it is saving another workbook:

Sub Create_New_MonthFile()
Dim Dys As Long, Curr As Long, i As Long
Dim FileName As String
Dim wb As Workbook
Set wb = ActiveWorkbook

FileName = strMonthShort & "-" & strMonthMed & "-" & strYearShort & ".xlsx"
Dys = Day(DateSerial(Year(Date), Month(Date) + 1, 0))
With Application
Curr = .SheetsInNewWorkbook
.SheetsInNewWorkbook = Dys
.Workbooks.Add
wb.SaveAs WED_MONTH_FOLDER & strYearFolder & FileName, FileFormat:=51
.SheetsInNewWorkbook = Curr
End With
For i = 1 To Dys
Sheets(i).Name = Ordinals(i)
Next i
End Sub
 
Upvote 0
In future when posting code please use code tags. How to Post Your VBA Code
You have a lot of variables that have no value. What are they meant to be?
 
Upvote 0
They are constants and public variables in another module
 
Upvote 0
In that case try
VBA Code:
Sub thardin()
   Dim Dys As Long, Curr As Long, i As Long
   Dim Fname As String
   
   Dys = Day(DateSerial(Year(Date), Month(Date) + 1, 0))
   Fname = strMonthShort & "-" & strMonthMed & "-" & strYearShort & ".xlsx"
   With Application
      Curr = .SheetsInNewWorkbook
      .SheetsInNewWorkbook = Dys
      .Workbooks.Add
      .SheetsInNewWorkbook = Curr
   End With
   For i = 1 To Dys
      Sheets(i).Name = Ordinals(i)
   Next i
   ActiveWorkbook.SaveAs WED_MONTH_FOLDER & strYearFolder & Fname, 51
End Sub
PS never use VBA keywords for the names of variables or procedures.
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,245
Members
448,952
Latest member
kjurney

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top