Check if worksheet tab name is already in use in another workbook

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hello folks,

I have been using this pair of code to check, if worksheet tab name exists.

It is based on the G2 value inside the Worksheet_One. (Worksheet_One is a part of workbook called Workbook_One).

VBA Code:
Function WorksheetExists2(WorksheetName As String, Optional wb As Workbook) As Boolean
    If wb Is Nothing Then Set wb = ThisWorkbook
    With wb
        On Error Resume Next
        WorksheetExists2 = (.Sheets(WorksheetName).Name = WorksheetName)
        On Error GoTo 0
    End With
End Function

VBA Code:
    If WorksheetExists2(Worksheets("Worksheet_One").Range("G2").Value) Then
    MsgBox "Tab name already exists.", vbCritical
    Exit Sub
    Else
    End If

Now I am trying to figure out a code, that will check existence of worksheet tab name in another Workbook based on cell value in different Workbook. Let me explain.

I have a workbook called Workbook_One with worksheet called Worksheet_One. Inside the Worksheet_One there is the cell G2 which is occupied with today's date.
There is also a workbook called Workbook_Two with multiple worksheets. Each worksheet tab is named with a specific date.

By runing this not-yet-existing-macro I want to check if worksheet tab name has been already used in Workbook_Two by the cell G2 value inside Worksheet_One, which is inside Workbook_One.

I hope I explained it well.

Help is much appreciated.


Thank you!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
How about
VBA Code:
If WorksheetExists2(Worksheets("Worksheet_One").Range("G2").Value, Workbooks("Workbook_Two.xlsm")) Then
Make sure the name of the other workbook is correct(including extension), also it needs to be open.
 
Upvote 0
Hi Fluff, thank you for your quick response.

Would it be possible to refer to Workbook_Two.xlsm not by its full name but by cell (A5) value?
 
Upvote 0
As long as A5 has the full name, yes. :)
 
Upvote 0
VBA Code:
   If WorksheetExists2(Worksheets("Worksheet_One").Range("G2").Value, Workbooks(Worksheets("Worksheet_One").Range("A5").Value)) Then

Should do the trick.
 
Upvote 0
Yup that should work. :)
 
Upvote 0
Solution

Forum statistics

Threads
1,214,833
Messages
6,121,862
Members
449,052
Latest member
Fuddy_Duddy

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