VBA code in Podcast 1072-Inserting Multilple Worksheets having names specified in a range

sajniho

New Member
Joined
Aug 8, 2009
Messages
3
I liked the video Podcast 1072 of Mr. Excel a lot.

A line of the VBA code

Set WSN = WBN.Worksheets.Add(after:=WBN.Worksheets(Ctr)

is incomplete when the video completes 2 minutes (see just above Bill's pic), Please help by mentioning the complete code of the above line here.

I do not know anything about the VBA. That would be a great help. Thanks in advance.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello and welcome to MrExcel.

I think it should be

Code:
Set WSN = WBN.Worksheets.Add(after:=WBN.Worksheets(Ctr))
 
Upvote 0
I liked the video Podcast 1072 of Mr. Excel a lot.

A line of the VBA code

Set WSN = WBN.Worksheets.Add(after:=WBN.Worksheets(Ctr)

is incomplete when the video completes 2 minutes (see just above Bill's pic), Please help by mentioning the complete code of the above line here.

I do not know anything about the VBA. That would be a great help. Thanks in advance.

Hello and welcome to MrExcel.

I think it should be

Rich (BB code):
Set WSN = WBN.Worksheets.Add(after:=WBN.Worksheets(Ctr))

Hi VoG,

Thank you for the welcome. I had already tried it before posting the query here but the VBA Editor is showing the error in this row. Following is the code that I have typed from the Podcast:

Rich (BB code):
Sub MakeWorkbook()
' code incomplete; Work-in-Progress

    Dim WBT As Workbook
    Dim WBN As Workbook
    Dim WSN As Worksheet
    Dim WST As Worksheet
    
    Set WBT = ActiveWorkbook
    Set WST = ActiveSheet
    
    Ctr = 1
    
    For Each cell In Selection
        If Ctr = 1 Then
            Set WBN = Workbooks.Add(xlWBATWorksheet)
            Set WSN = WBN.Worksheets(1)
        Else
            Set WSN = WBN.Worksheets.Add(after:=WBN.Worksheets(Ctr))
        End If
        On Error Resume Next
        WSN.Name = cell.Value
        On Error GoTo 0
        WSN.Cells(1, 1).Value = cell.Value
        Ctr = Ctr + 1
    Next cell
    
    WBN.Worksheets(1).Select
End Sub
 
Upvote 0
Hi. Try this - it works for me:

Rich (BB code):
Sub MakeWorkbook()
' code incomplete; Work-in-Progress

    Dim WBT As Workbook
    Dim WBN As Workbook
    Dim WSN As Worksheet
    Dim WST As Worksheet
    Dim Ctr As Integer '<< added
    Dim cell As Range '<< added
    
    Set WBT = ActiveWorkbook
    Set WST = ActiveSheet
    
    Ctr = 1
    
    For Each cell In Selection
        If Ctr = 1 Then
            Set WBN = Workbooks.Add(xlWBATWorksheet)
            Set WSN = WBN.Worksheets(1)
        Else
            Set WSN = WBN.Worksheets.Add(after:=WSN)
        End If
        On Error Resume Next
        WSN.Name = cell.Value
        On Error GoTo 0
        WSN.Cells(1, 1).Value = cell.Value
        Ctr = Ctr + 1
    Next cell
    
    WBN.Worksheets(1).Select
End Sub
 
Upvote 0
Incomple line from Podcast 1072 Does anyone know what the rest of the line of code should be without changing the coding.

Set WSN = WBN.Worksheets.Add(after:=WBN.Worksheets(Ctr):


Thanks in advance

 
Upvote 0
Try

Code:
Set WSN = WBN.Worksheets.Add(after:=WBN.Worksheets(Ctr - 1))
 
Upvote 0
Is it possible to modify this code so that the worksheets are added to the original workbook, rather than creating a new workbook when the macro is ran.

Also, can I assign the macro to use a specific worksheet template when it creates new worksheets?

Thank you!


Hi. Try this - it works for me:

Rich (BB code):
Sub MakeWorkbook()
' code incomplete; Work-in-Progress

    Dim WBT As Workbook
    Dim WBN As Workbook
    Dim WSN As Worksheet
    Dim WST As Worksheet
    Dim Ctr As Integer '<< added
    Dim cell As Range '<< added
    
    Set WBT = ActiveWorkbook
    Set WST = ActiveSheet
    
    Ctr = 1
    
    For Each cell In Selection
        If Ctr = 1 Then
            Set WBN = Workbooks.Add(xlWBATWorksheet)
            Set WSN = WBN.Worksheets(1)
        Else
            Set WSN = WBN.Worksheets.Add(after:=WSN)
        End If
        On Error Resume Next
        WSN.Name = cell.Value
        On Error GoTo 0
        WSN.Cells(1, 1).Value = cell.Value
        Ctr = Ctr + 1
    Next cell
    
    WBN.Worksheets(1).Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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