Check if exists with custom code VBA

LowRez

New Member
Joined
Mar 11, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hello,

Total beginner in VBA.
I`ve been searching the forums without any luck.

First of all, let me explain what i did and what i`m trying to achieve.
I`m trying to do some sort of attendance worksheet in Excel 2019..
So far i`ve been successful following this video :
On the COPY SHEET button (min. 29:20), i`ve pasted the code in the VBA and it does work.
This is my code:


Sub Copyrenameworksheet()
'Updateby Extendoffice
Dim ws As Worksheet
Set wh = Worksheets(ActiveSheet.Name)
ActiveSheet.Copy After:=Worksheets(Sheets.Count)
If wh.Range("E5").Value <> "" Then
ActiveSheet.Name = wh.Range("E5").Value
End If
wh.Activate
End Sub


What i want to do now, is that i want to check if that "E5" sheet name already exists first. If it does it should return a message, if not, create one.
Any help please?
Total noob in programming. :)

Thank you.
 

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
First off you defined a Worksheet variable as ws but set wh. If it didn't stop you for that then you have not made your code option explicit. You should go to Tools->Options->Require variable declaration. That way you will catch little things like your worksheet variable. Now to your question. The worksheets are a collection so you can go through the collection and check the name of the sheet. Return true if you find it false if you don't.

Code:
Public Function SheetExists(ByVal sheetName As String) As Boolean

    'Define the varaible
    Dim ws As Worksheet
    SheetExists = False
    'Start going through the collection of sheets
    For Each ws In ThisWorkbook.Sheets
        
        If ws.name = sheetName Then
            'If the sheet name matches the name passed in, then you found it.
            'Set the return to true and get out.
            SheetExists = True
            Exit For
        End If
    Next
        
End Function
 
Upvote 0
How about
VBA Code:
Sub Copyrenameworksheet()
   Dim Ws As Worksheet
   
   Set Ws = ActiveSheet
   If Ws.Range("E5").Value = "" Then
      MsgBox "E5" Is blank
      Exit Sub
   End If
   If Not Evaluate("isref('" & Ws.Range("E5").Value & "'!A1)") Then
      MsgBox "sheet already exists"
      Exit Sub
   End If
   Ws.Copy After:=Worksheets(Sheets.Count)
   ActiveSheet.Name = Ws.Range("E5").Value
   Ws.Activate
End Sub
 
Upvote 0
Solution
Sub Copyrenameworksheet() Dim Ws As Worksheet Set Ws = ActiveSheet If Ws.Range("E5").Value = "" Then MsgBox "E5" Is blank Exit Sub End If If Not Evaluate("isref('" & Ws.Range("E5").Value & "'!A1)") Then MsgBox "sheet already exists" Exit Sub End If Ws.Copy After:=Worksheets(Sheets.Count) ActiveSheet.Name = Ws.Range("E5").Value Ws.Activate End Sub
Compile error:
Type mismatch...

This E5 is a drop down list containing a month.. guessing E5 is a variable that should return what`s inside that E5 cell?
 
Upvote 0
Oops, misplaced " it should be
VBA Code:
      MsgBox "E5 Is blank"
 
Upvote 0
Oops, misplaced " it should be
VBA Code:
      MsgBox "E5 Is blank"
Thank you,

Closer, but now with only my main sheet (named completely different), it gives me the "sheet already exists" MsgBox .
 
Upvote 0
It should be
VBA Code:
   If Evaluate("isref('" & Ws.Range("E5").Value & "'!A1)") Then
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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