Copy Tab and rename based on VBA input box

Darren77

New Member
Joined
Jun 29, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello

I am quite new to VBA and can navigate myself around it (sort of) but have come up stuck with this issue. What i am attempting to do is

1. copy sheet6
2. rename the new sheet based on the input box text
3. if there is an error (sheet name exists) then go back to input box to rename.

I get a couple of issues in so much with the error handle a new sheet gets created no matter what which then puts me into the endless loop until i choose another name. The end result is a lot of duplicated sheets i dont need.

Please help!!

Dim myNewSheetName
tryagain:

On Error GoTo err

myNewSheetName = InputBox("Please enter new TAB name." & vbCrLf & _
"Please use SAP Reference as TAB name" & vbCrLf & _
"Example: 1.1.1")
On Error GoTo err
Sheet6.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = myNewSheetName
ActiveSheet.Tab.ColorIndex = 43
Exit Sub
err:
MsgBox "SAP already Exists"
Resume tryagain:

end sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,998
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Darren()
   Dim ShtName As String
   
   Do
      ShtName = InputBox("Please enter new TAB name." & vbCrLf & _
            "Please use SAP Reference as TAB name" & vbCrLf & _
            "Example: 1.1.1")
      If ShtName = "" Then Exit Sub
   Loop Until Evaluate("Isref('" & ShtName & "'!A1)") = False
   Sheet6.Copy , Sheets(Sheets.Count)
   With ActiveSheet
      .Name = ShtName
      .Tab.ColorIndex = 43
   End With
End Sub
 
Solution

Darren77

New Member
Joined
Jun 29, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Darren()
   Dim ShtName As String
  
   Do
      ShtName = InputBox("Please enter new TAB name." & vbCrLf & _
            "Please use SAP Reference as TAB name" & vbCrLf & _
            "Example: 1.1.1")
      If ShtName = "" Then Exit Sub
   Loop Until Evaluate("Isref('" & ShtName & "'!A1)") = False
   Sheet6.Copy , Sheets(Sheets.Count)
   With ActiveSheet
      .Name = ShtName
      .Tab.ColorIndex = 43
   End With
End Sub
Hello Fluff

thanks for the welcome and many thanks for the solution. This works perfectly.

Many thanks
Darren
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,998
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,148,252
Messages
5,745,674
Members
423,967
Latest member
malayaka

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
Top