code not adding sheets

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
My code is not adding a sheet, and it looks like it should. It only has the one sheet called July. What have I done wrong?

VBA Code:
Sub AddYP()
Dim NewYP As String
    NewYP = Tracker.Cells(5, 4)
    YP.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = NewYP
    Call CreateWB(NewYP)
End Sub
Sub CreateWB(NewYP As String)
Dim wb1 As Workbook
    Workbooks.Add.SaveAs Filename:=ThisWorkbook.Path & "\Young People\" & NewYP, FileFormat:=52
    With Workbooks(NewYP)
        Sheets(1).Name = "July"
        Sheets.Add '.Name = "August"
    End With
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
VBA Code:
With activeworkbook
        Sheets(1).Name = "July"
        Sheets.Add.Name = "August"
end with
 
Upvote 0
Once you created a new workbook, the new one is an active workbook. So you just do like this
VBA Code:
Sub CreateWB(NewYP As String)
Dim wb1 As Workbook
    Workbooks.Add.SaveAs Filename:=ThisWorkbook.Path & "\Young People\" & NewYP, FileFormat:=52
    Sheets(1).Name = "July"
    Sheets.Add '.Name = "August"
End Sub

I see that you also have unused wb1 declared
 
Upvote 0
And I'm guessing you would want sheet "August" AFTER "July"
VBA Code:
With ActiveWorkbook
        Sheets(1).Name = "July"
        Sheets.Add(After:=Sheets("July")).Name = "August"
End With
 
Upvote 0
Your With statement isn't really doing anything, try this.
VBA Code:
Sub AddYP()
Dim NewYP As String

    NewYP = Tracker.Cells(5, 4)
    YP.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = NewYP
    Call CreateWB(NewYP)
    
End Sub

Sub CreateWB(NewYP As String)
Dim wb1 As Workbook

    Set wb1 = Workbooks.Add
    wb1.SaveAs Filename:=ThisWorkbook.Path & "\Young People\" & NewYP, FileFormat:=52
    With wb1
        .Sheets(1).Name = "July"
        .Sheets.Add.Name = "August"
    End With
    
End Sub
 
Upvote 0
Thanks for your help guys, I got it to work with this code (y)

VBA Code:
Sub CreateWB(NewYP As String)
Dim AddWb As Workbook
    Workbooks.Add.SaveAs Filename:=ThisWorkbook.Path & "\Young People\" & NewYP, FileFormat:=52
    With Workbooks(NewYP)
        .Sheets(1).Name = "July"
        .Sheets.Add.Name = "August"
        .Sheets.Add.Name = "September"
        .Sheets.Add.Name = "October"
        .Sheets.Add.Name = "November"
        .Sheets.Add.Name = "December"
        .Sheets.Add.Name = "January"
        .Sheets.Add.Name = "February"
        .Sheets.Add.Name = "March"
        .Sheets.Add.Name = "April"
        .Sheets.Add.Name = "May"
        .Sheets.Add.Name = "June"
    End With
End Sub
 
Upvote 0
you could have also used
VBA Code:
Sub MM1()
    Dim i As Long
    Workbooks.Add.SaveAs Filename:=ThisWorkbook.Path & "\Young People\" & NewYP, FileFormat:=52
    For i = 1 To 12
        With Sheets.Add(, Sheets(i))
            .Name = MonthName(i)
        End With
    Next i
End Sub
 
Upvote 0
Thanks Michael but would that put the month order in a financial year format, July-June?
 
Upvote 0
Yep....click and drag
Select Months Dec to June and drag them to the fron of the "queue"
BUT my code does....Jan to Dec
 
Upvote 0
This is the code I have. It creates a sheet1 first then the sheets from January to December.

I want it to be just the sheets July-June

VBA Code:
Sub CreateWB(NewYP As String)
Dim i As Long
    If Cells(5, 4) = "" Then
        MsgBox "Please enter a name of the young person you want to add in the name field at D5"
        Exit Sub
    End If
    Call CheckFolderExists
    Workbooks.Add.SaveAs Filename:=ThisWorkbook.Path & "\Young People\" & NewYP, FileFormat:=52
    For i = 1 To 12
        With Sheets.Add(, Sheets(i))
            .Name = MonthName(i)
        End With
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,604
Members
449,109
Latest member
Sebas8956

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