VBA - No Duplicate Sheet

  • Thread starter Thread starter jordanctc
  • Start date Start date
J

jordanctc

Simple problem with (hopefully) a simple answer

I have a macro that creates a new sheet and names it the contents of
strTitle. However, it would be easy for someone to fire off the macro
- not realizing there is a sheet of that name already in existance.
Thusly, adding a few lines of code to check for the sheet and stop the
macro from crashing would be wonderful. However, it's a bit out of my
experience and I would appreciate any help. I couldn't find anything
that I could get to work from archives.

Jordan
 
Wedge this code in there somewhere before you rename the sheet.

Dim s
Dim bFound As Boolean

bFound = False
For Each s In Worksheets
If LCase(s.Name) = LCase(strTitle) Then bFound = True
Next

If bFound = True Then
MsgBox "You already have a sheet named " & strTitle & ". More inf
here..."
Exit Sub
End If

'Your normal code here
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top