Copy and rename worksheet based on specified cell value

Kenor

Board Regular
Joined
Dec 8, 2020
Messages
116
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I want to set button click to copy and rename worksheet based on specified cell value

for example cell K1 = A Press and cell L1 = 16062022

So new worksheet = A Press 16062022

I would like to use the below code but need to modify it a bit. I need someone to help.

Sub Copyrenameworksheet()
Dim ws As Worksheet
Set wh = Worksheets(ActiveSheet.Name)
ActiveSheet.Copy After:=Worksheets(Sheets.Count)
If wh.Range("A1").Value <> "" Then
ActiveSheet.Name = wh.Range("A1").Value
End If
wh.Activate
Ens Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
How about
VBA Code:
Sub Kenor()
   Dim ShtName As String
   
   ShtName = Range("K1").Value & " " & Range("L1").Value
   If ShtName = "" Then
      MsgBox "no name entered"
      Exit Sub
   ElseIf Evaluate("isref('" & ShtName & "'!A1)") Then
      MsgBox "Sheet name " & ShtName & " is already used"
      Exit Sub
   End If
   ActiveSheet.Copy , Sheets(Sheets.Count)
   ActiveSheet.Name = ShtName
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub Kenor()
   Dim ShtName As String
  
   ShtName = Range("K1").Value & " " & Range("L1").Value
   If ShtName = "" Then
      MsgBox "no name entered"
      Exit Sub
   ElseIf Evaluate("isref('" & ShtName & "'!A1)") Then
      MsgBox "Sheet name " & ShtName & " is already used"
      Exit Sub
   End If
   ActiveSheet.Copy , Sheets(Sheets.Count)
   ActiveSheet.Name = ShtName
End Sub

Thanks Fluff...it's great! :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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