Adding a sheet and checking it does not already exist when is hidden

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,210
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

I'm getting a bit confused and need help (so my friends keep telling me :-) )

I have a workbook and want to create a new sheet using vba if the sheets not already there but the sheets are hidden?

so any ideas how i can do this?

lets say the sheet is called "Tony1"

Thanks

Tony
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Re: Addibg a sheet and checking it does not already exist when is hidden

Try this macro. It will detect whether or not the sheet already exists. It doesn't matter if it's hidden or not.
Code:
Sub Tony()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, response As String
    response = InputBox("Please enter the name of the sheet to add.")
    Set ws = Nothing
    On Error Resume Next
    Set ws = Worksheets(response)
    On Error GoTo 0
    If ws Is Nothing Then
        Worksheets.Add(After:=Sheets(Sheets.Count)).Name = response
    Else
        MsgBox ("A sheet already exists with the name " & response & ".")
    End If
End Sub
 
Upvote 0
Re: Addibg a sheet and checking it does not already exist when is hidden

Hi,
similar

Code:
Sub AddSheet()
    Dim SheetName As Variant
    Do
        SheetName = InputBox("Please Enter Sheet Name To Add", "Add Sheet")
'cancel pressed
        If StrPtr(SheetName) = 0 Then Exit Sub
        If Len(SheetName) > 0 Then
            If Not Evaluate("ISREF('" & SheetName & "'!A1)") Then
                Exit Do
            Else
                MsgBox SheetName & Chr(10) & "Sheet Name Exists", 48, "Sheet Exists"
            End If
        End If
    Loop


    Worksheets.Add(After:=Sheets(Sheets.Count)).Name = SheetName


End Sub

Dave
 
Last edited:
Upvote 0
Re: Addibg a sheet and checking it does not already exist when is hidden

Thank you very much this is very helpful,
I don't need it to tell me if the sheet exists just get on with the macro but look at both your codes I can work out how to do what I wanted so amasive thank you to both of you for your help.
Thanks
Tony
 
Upvote 0
Re: Addibg a sheet and checking it does not already exist when is hidden

welcome - glad we could help

Many thanks for feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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