Macro to always add second sheet tab ?

zone709

Well-known Member
Joined
Mar 1, 2016
Messages
2,094
Office Version
  1. 365
Platform
  1. Windows
Hi so what I am trying to do below is just add another tab, but I need to always add a tab and it to be second from first.

I have 15 tabs so I always want to add a new one as second tab and name it Errors. I tried worksheets (2) but debug.

Code:
[CODE]

Sub Macro3()

'
    Sheets.Add After:=ActiveSheet
    Sheets("Sheet1").Select
    Sheets("Sheet1").Name = "Errors"
End Sub
[/CODE]
 

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
How about
Code:
Sub Macro3()
    Sheets.Add(, Sheets(1)).name = "Errors"
End Sub
This assumes you will never have an existing sheet called Errors.
 
Upvote 0
1. Are you replacing another worksheet named "errors"? (and how are you not if you are always adding another? Is this a template?)
2. Is your first worksheet named? Would you like to reference that vs. Activesheet?
 
Upvote 0
Hi no i dont have an errors tab on any other sheet. What Fluff gave me works fine. I was needing something like this just in case I have lets say last tab selected. It will always send it to second thanks guys
 
Upvote 0
Hi I actually ran into that issue. Since sometimes I will forget to delete the error tab. Can we tweak this. If the Error tab it there then skip this below?

Code:
[LEFT][COLOR=#333333][FONT=monospace]Sub Macro3()
    Sheets.Add(, Sheets(1)).name = "Errors"
End Sub[/FONT][/COLOR][/LEFT]
 
Upvote 0
Try
Code:
Sub Macro3()
   If Not Evaluate("isref(Errors!A1)") Then Sheets.Add(, Sheets(1)).name = "Errors"
End Sub
 
Upvote 0
Fluff rocks. Mine is written in crayon, but I'm sure can be fixed up. Not happy about the multiple if statements, but my do while was not satisfactory for me.

Code:
 
Last edited:
Upvote 0
Oops. Just for the fun of it.

Code:
Option Explicit
Private Sub CreateSheet(sNewSheetName As String)
    Dim wbActive As Workbook
    Dim wsFirst As Worksheet, wsNew As Worksheet
    
    Set wbActive = ThisWorkbook
    Set wsFirst = Worksheets(1)
    
    If Not VerifySheet(sNewSheetName) Then
        Set wsNew = wbActive.Sheets.Add(After:=wsFirst)
        wsNew.Name = sNewSheetName
        Else
            sNewSheetName = InputBox("Please enter a different sheet name to add")
            If Not VerifySheet(sNewSheetName) Then
                Set wsNew = wbActive.Sheets.Add(After:=wsFirst)
                wsNew.Name = sNewSheetName
                Else
                    sNewSheetName = MsgBox("Please erase some of the errant sheets")
                End If
    End If
    wsFirst.Activate
End Sub




 Private Function VerifySheet(sSName As String, Optional wbCurrent As Workbook) As Boolean
    Dim sSetSheet As Worksheet


     If wbCurrent Is Nothing Then Set wbCurrent = ActiveWorkbook
     On Error Resume Next
     Set sSetSheet = wbCurrent.Sheets(sSName)
     On Error GoTo 0
     VerifySheet = Not sSetSheet Is Nothing
 End Function




Private Sub CommandButton1_Click()
    Call CreateSheet("Errors")
End Sub
 
Upvote 0
Oops. Just for the fun of it.

Code:
Option Explicit
Private Sub CreateSheet(sNewSheetName As String)
    Dim wbActive As Workbook
    Dim wsFirst As Worksheet, wsNew As Worksheet
    
    Set wbActive = ThisWorkbook
    Set wsFirst = Worksheets(1)
    
    If Not VerifySheet(sNewSheetName) Then
        Set wsNew = wbActive.Sheets.Add(After:=wsFirst)
        wsNew.Name = sNewSheetName
        Else
            sNewSheetName = InputBox("Please enter a different sheet name to add")
            If Not VerifySheet(sNewSheetName) Then
                Set wsNew = wbActive.Sheets.Add(After:=wsFirst)
                wsNew.Name = sNewSheetName
                Else
                    sNewSheetName = MsgBox("Please erase some of the errant sheets")
                End If
    End If
    wsFirst.Activate
End Sub




 Private Function VerifySheet(sSName As String, Optional wbCurrent As Workbook) As Boolean
    Dim sSetSheet As Worksheet


     If wbCurrent Is Nothing Then Set wbCurrent = ActiveWorkbook
     On Error Resume Next
     Set sSetSheet = wbCurrent.Sheets(sSName)
     On Error GoTo 0
     VerifySheet = Not sSetSheet Is Nothing
 End Function




Private Sub CommandButton1_Click()
    Call CreateSheet("Errors")
End Sub

Thanks guys again for the help :)
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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