Undo VBA actions if user doesn't enter a value in an inputbox

Bhemi

New Member
Joined
Jul 26, 2014
Messages
5
I created a Macro with a button that copies a hidden master sheet, then prompts the user to enter a sheet name with an input box. The button works if the user puts in a name, but if they press cancel, exit out, or just press OK, without a name; the macro goes to the code with an error. When I try to troubleshoot the issue I can only find ways to have the macro copy the sheet and name it Master (2). The problem is the Macro creates a copy first, then the input box appears.
Possible solutions:
  • Undo the actions the Macro already accomplished
  • Name the sheet for the user. This would be the desired solution, but I cant figure out a way to have VBA select the proper name. It is just a string of numerical names: M1,M2,M3...

Sub Macro7()
'
' Macro7 Macro
'
' Keyboard Shortcut: Ctrl+n
'
Application.ScreenUpdating = False
Sheets("Measure (master)").Visible = True
Sheets("Measure (master)").Select
Sheets("Measure (master)").Copy Before:=Sheets("Measure (Master)")
Sheets("Measure (master) (2)").Name = InputBox("Sheet Name", "Enter sheet name", "")
Sheets("Measure (master)").Select
ActiveWindow.SelectedSheets.Visible = False
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Why not just move the inputbox first? But, if you want to force a name, use this.

Code:
Sub Macro7()'
' Macro7 Macro
'
' Keyboard Shortcut: Ctrl+n
'
Application.ScreenUpdating = False


Dim nameVar As String


With ThisWorkbook.Sheets("Measure (master)")
    .Visible = True
    .Copy Before:=Sheets("Measure (master)")
End With


With ActiveSheet
askAgain:
    nameVar = InputBox("Sheet Name", "Enter Sheet Name:", "")
    If nameVar = "" Then
        GoTo askAgain
    Else
        .name = nameVar
    End If
End With


With Sheets("Measure (master)")
    .Visible = False
End With


Application.ScreenUpdating = True


End Sub
 
Upvote 0
Your code worked perfectly, Thank you! Do you know if the other Solution I had would be executable? I would want the user to hit the New Sheet button and a new sheet is created with the correct name. The names would just go in sequence from M1 to M30. If not this will work fine.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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