Copy Tabs with VBA

roboed

New Member
Joined
Nov 30, 2018
Messages
2
Hi.

I am trying to write a simple code which does the following, with the click of a button on its respective tab.

1. Unprotect all sheets with a password
2. Copies the selected / active sheet to a new sheet, placed at the end of existing sheets
3. Displays a message box to name the new sheet
4. Protect all sheets with a password

Reason for nr 1 and 4 is error messages when copying and moving between the sheets while protected - this trick removed the error. And the sheets needs to be protected for "idiot-proofing" reasons.

But, the wrong sheet gets copied. Assume I have 5 sheets: A, B, C, D and E.
If my active sheet is "E", and I press my macro-connected button it copies "D".
If my active sheet is "A", and I press my macro-connected button it still copies "D".

I suspect steps 1 and/or 4 is messing with something...

Fairly new to coding, so cut and paste from various online sites + trying and failing is my method of working. But great fun! :)

Any tips or tricks?

Code:

Sub

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
ws.Unprotect "password123"
Next ws


ActiveSheet.Copy After:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = InputBox("Name of New Invoice Tab:")

For Each ws In ThisWorkbook.Worksheets
ws.Protect "password123"
Next ws

End Sub
 

Some videos you may like

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

roboed

New Member
Joined
Nov 30, 2018
Messages
2
Hi.

I am trying to write a simple code which does the following, with the click of a button on its respective tab.

1. Unprotect all sheets with a password
2. Copies the selected / active sheet to a new sheet, placed at the end of existing sheets
3. Displays a message box to name the new sheet
4. Protect all sheets with a password

Reason for nr 1 and 4 is error messages when copying and moving between the sheets while protected - this trick removed the error. And the sheets needs to be protected for "idiot-proofing" reasons.

But, the wrong sheet gets copied. Assume I have 5 sheets: A, B, C, D and E.
If my active sheet is "E", and I press my macro-connected button it copies "D".
If my active sheet is "A", and I press my macro-connected button it still copies "D".

I suspect steps 1 and/or 4 is messing with something...

Fairly new to coding, so cut and paste from various online sites + trying and failing is my method of working. But great fun! :)

Any tips or tricks?

Code:

This seemed to fix it :)

Sub

Dim ws As Worksheet


ActiveSheet.Unprotect "password123"

ActiveSheet.Copy After:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = InputBox("Name of New Invoice Tab:")

For Each ws In ThisWorkbook.Worksheets
ws.Protect "password123"
Next ws

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,102,867
Messages
5,489,361
Members
407,686
Latest member
Chuck1960

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top