Auto rename worksheet, save with password to desktop.

Graham C1600

Board Regular
Joined
Feb 17, 2018
Messages
96
Office Version
  1. 365
Ok so here goes.

I have a spreadsheet where I need to calculate various things and then finally need to do the following :-
  1. Create a copy of the worksheet in a new book.
  2. Rename the worksheet based on Cells A2 & A3 (these are only names)
  3. Save the worksheet to my desktop and name it as per cells A2 & A3.
  4. Put a password on to open the spreadsheet 'Password 123'. I can change this at a later date.
I've tried via a macro but cannot seem to get it to work.

I will put a Form Control Button on the worksheet and then hopefully just put the code onto this button.

Hope this makes sense.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try:

VBA Code:
Sub CopyandSave()

Dim wb As Workbook
Dim ws As Worksheet
Dim myname As String

Set ws = ActiveSheet
myname = ws.Range("A2") & ws.Range("A3")

ws.Name = myname
ws.Copy
Set wb = ActiveWorkbook
wb.Protect Password:="Password 123", Structure:=True, Windows:=False
wb.SaveAs "C:\Users\your.name\Desktop\" & myname & ".xlsx"


End Sub
 
Upvote 0
Try:

VBA Code:
Sub CopyandSave()

Dim wb As Workbook
Dim ws As Worksheet
Dim myname As String

Set ws = ActiveSheet
myname = ws.Range("A2") & ws.Range("A3")

ws.Name = myname
ws.Copy
Set wb = ActiveWorkbook
wb.Protect Password:="Password 123", Structure:=True, Windows:=False
wb.SaveAs "C:\Users\your.name\Desktop\" & myname & ".xlsx"


End Sub
Thanks for the reply. Can you confirm what I need to do with these 2 ? Do I just need 1 ?

Sub Button1_Click()
Sub CopyandSave()

And then just one End Sub at the end.

Thanks
 
Upvote 0
I believe this will work

VBA Code:
Sub Button1_Click ()

Dim wb As Workbook
Dim ws As Worksheet
Dim myname As String

Set ws = ActiveSheet
myname = ws.Range("A2") & ws.Range("A3")

ws.Name = myname
ws.Copy
Set wb = ActiveWorkbook
wb.Protect Password:="Password 123", Structure:=True, Windows:=False
wb.SaveAs "C:\Users\your.name\Desktop\" & myname & ".xlsx"


End Sub
 
Upvote 0
I believe this will work

VBA Code:
Sub Button1_Click ()

Dim wb As Workbook
Dim ws As Worksheet
Dim myname As String

Set ws = ActiveSheet
myname = ws.Range("A2") & ws.Range("A3")

ws.Name = myname
ws.Copy
Set wb = ActiveWorkbook
wb.Protect Password:="Password 123", Structure:=True, Windows:=False
wb.SaveAs "C:\Users\your.name\Desktop\" & myname & ".xlsx"


End Sub
Hi,

Thanks for this. So a couple of things don't seem right :-
Firstly when i use the code it renames the original sheet and also the new sheet from Range A2 & A3. I only need the new sheet renamed.
Secondly the password protection needs to be on so you have to input the password to open the sheet. It currently opens with the above code. I can cancel the password and type onto the sheet.

Hope this makes sens.

Thanks for your help so far.
 
Upvote 0

Forum statistics

Threads
1,214,859
Messages
6,121,963
Members
449,059
Latest member
oculus

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