How to stop the cancel button from carrying out the sub?

Balrajss0121

New Member
Joined
May 6, 2020
Messages
11
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi, I am fairly new to vba and am trying to work through a spreadsheet. At the moment I have managed to create a macro via vba as shown below. It currently works via pressing the required button and an input box being displayed which asks for the name of the new sheet to be created. The new sheet is copied from a hidden sheet called "MASTER SHEET". The issue I am having is that when I press cancel or the close button on the input box, a new sheet is still created given the name MASTER SHEET(1) etc. Any help would be appreciated in rectifying the issue :)

The code is as follows:

Sub CopySheetAndRename()

Dim ActNm As String
Application.ScreenUpdating = False
On Error Resume Next
ActiveWorkbook.Sheets("MASTER SHEET").Visible = True
ActiveWorkbook.Sheets("MASTER SHEET").Copy _
after:=ActiveWorkbook.Sheets("MASTER SHEET")
ActNm = ActiveSheet.Name
ActiveSheet.Name = InputBox("Enter the name for the new sheet.")
Sheets(ActiveSheet.Name).Visible = True
ActiveWorkbook.Sheets("MASTER SHEET").Visible = False
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub CopySheetAndRename()
   Dim ActNm As String
   
   ActNm = InputBox("Enter the name for the new sheet.")
   If ActNm = "" Then Exit Sub
   Application.ScreenUpdating = False
   With ActiveWorkbook
      .Sheets("MASTER SHEET").Visible = True
      .Sheets("MASTER SHEET").Copy , .Sheets("MASTER SHEET")
      ActiveSheet.Name = ActNm
      .Sheets("MASTER SHEET").Visible = False
   End With
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
Sub CopySheetAndRename()
   Dim ActNm As String
  
   ActNm = InputBox("Enter the name for the new sheet.")
   If ActNm = "" Then Exit Sub
   Application.ScreenUpdating = False
   With ActiveWorkbook
      .Sheets("MASTER SHEET").Visible = True
      .Sheets("MASTER SHEET").Copy , .Sheets("MASTER SHEET")
      ActiveSheet.Name = ActNm
      .Sheets("MASTER SHEET").Visible = False
   End With
   Application.ScreenUpdating = True
End Sub

Thank you so much, that worked a charm. My only other query which I just stumbled upon is to stop an error being thrown when a duplicate name is used as instead tell the user to use another name. I appreciate your help greatly :)
 
Upvote 0
Ok, how about
VBA Code:
Sub CopySheetAndRename()
   Dim ActNm As String
   
   ActNm = InputBox("Enter the name for the new sheet.")
   If ActNm = "" Then Exit Sub
   If Evaluate("isref('" & ActNm & "'!A1)") Then
      MsgBox "That name is taken"
      Exit Sub
   End If
   Application.ScreenUpdating = False
   With ActiveWorkbook
      .Sheets("MASTER SHEET").Visible = True
      .Sheets("MASTER SHEET").Copy , .Sheets("MASTER SHEET")
      ActiveSheet.Name = ActNm
      .Sheets("MASTER SHEET").Visible = False
   End With
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub CopySheetAndRename()
   Dim ActNm As String
  
   ActNm = InputBox("Enter the name for the new sheet.")
   If ActNm = "" Then Exit Sub
   If Evaluate("isref('" & ActNm & "'!A1)") Then
      MsgBox "That name is taken"
      Exit Sub
   End If
   Application.ScreenUpdating = False
   With ActiveWorkbook
      .Sheets("MASTER SHEET").Visible = True
      .Sheets("MASTER SHEET").Copy , .Sheets("MASTER SHEET")
      ActiveSheet.Name = ActNm
      .Sheets("MASTER SHEET").Visible = False
   End With
   Application.ScreenUpdating = True
End Sub
Thanks that worked perfectly too. Is there any chance you could explain the code you just added for the duplicate name, so I understand fully for next time :)
 
Upvote 0
It uses the worksheet function ISREF to test if cell A1 exists on that sheet (ie does the sheet exist). The evaluate allows VBA to calculate a string formula & will return true or false depending on whether the sheet exists.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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