Check if Sheet Name Exists Before Renaming

DanGK1

Board Regular
Joined
Jul 7, 2010
Messages
60
Hi

I am importing a worksheet from another file and ultimately renaming that imported sheet. I am happy with the import of the sheet and I can format the look and feel but before the sheet is renamed, I would like to check if the planned sheet name exists and if it does append an incremental number to the sheet name. ie if "Functionality" already exists then the newest sheet would be called "Functionality(1)" or "Functionality_1" etc.

This is my existing code and I would assume the check and increment would go in the parts that say activesheet.name

Code:
Sub FormatData()

lRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

With ActiveSheet
.ListObjects.Add(xlSrcRange, Range(Cells(1, 1), Cells(lRow, lCol)), , xlYes).Name = "ISV Data"
.ListObjects("ISV Data").TableStyle = "TableStyleMedium13"
.Columns.AutoFit
.Rows.AutoFit
.Cells.VerticalAlignment = xlTop
End With
NewSheetName = MsgBox("Is this a funcationality import?", vbYesNo + vbQuestion, "Import Type")
If NewSheetName = vbYes Then
ActiveSheet.Name = "ISV Functionality"
Else
ActiveSheet.Name = "ISV Summary"
Range("A1").Select
End If
ImportAnother = MsgBox("Import Another Export?", vbYesNo + vbQuestion, "Import Another?")
If ImportAnother = vbYes Then
Call ImportISVExport
Else
Call CopySheets
End If

End Sub

I would appreciate any guidance.

Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This is how you check for an existing sheet

VBA Code:
Sub jec()
 If Not IsError(Evaluate("'your sheet name'!A1")) Then
   MsgBox "sheet does exist already"
 Else
   MsgBox "does Not exist"
 End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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