VBA Check if sheet exists

Klturi421

Board Regular
Joined
Aug 31, 2014
Messages
52
I am building a form to create new employees profile, when I do so I would like to check to see if the sheet exists first and give a warning so changes could be made as necessary. All I am looking for at the moment is a simple if it exists msgbox else msgbox.

Any assistance would be appreciated.
 
Thanks a lot Fluff

Code is working fine but the newly created sheet is also getting protected. As per your code from sheet 2 onwards it should protect but sheet 1 is also protected. Any guess.

Also is there a way to hide the password written in the code? One way is to protect the module from viewing altogether.
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
My best guess as to why it's getting protected is that Sheets(1) is a hidden sheet.
AFAIK there is no way of hiding the password in the code.
This should resolve the protection issue
Code:
Sub FinalizeReport()
Dim ws1 As Worksheet, Ws As Worksheet
Dim Nme As String

Set ws1 = ThisWorkbook.Worksheets("Master")
Nme = format(Date, "dd-mm-yyyy")
If Evaluate("isref('" & Nme & "'!A1)") Then Exit Sub

ws1.Copy Before:=ThisWorkbook.Sheets(1)
ActiveSheet.name = Nme

For Each Ws In ThisWorkbook.Worksheets
   If Not Ws.name = Nme Then Ws.Protect Password:="password"
Next
End Sub
 
Upvote 0
I noticed that both of your codes working if Master sheet is unprotected. After first run, when Master gets protected, newly created sheet also getting protected.
Looks like we need to keep Master unprotected always.
 
Upvote 0
In that case try
Code:
For Each Ws In ThisWorkbook.Worksheets
   If Not Ws.name = Nme And Not Ws.name = "Master" Then Ws.Protect Password:="password"
Next
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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