Prevent sheets renaming, moving and deletion - excel

I have a workbook consisting of 44 sheets. In this workbook, I have a VBA code that protects and unprotects the sheets, hides and unhides sheets, inserts and deletes inserted sheets.
How do I protect the workbook such that a user doesn't delete, rename or even move the sheets?
I tried the protect workbook structure but it prevents insertion of sheets. How do I go about this?

One thing you could do is rename and move the Sheet any time it is activated. Each worksheet has a worksheet name(Sheet1) and a Caption("MyWorksheet"). If the user changes the caption, you will not be able to reference the caption, so reference the sheet name instead. You can set a sheet name or just reference the default sheet name. In the VBA IDE, in the VBAProject open the Micorsoft Excel Objects folder in the Project window, and open the sheet instead of the Modules. In the dropdown at the top select Worksheet, then in the second drop down on the right select Activate. This will generate the following code, or you could just copy paste the following into the page. I added the renaming of the sheet caption and the move sheet code.
Option Explicit
Private Sub Worksheet_Activate()
On Error Resume Next
Sheet5.Name = "TEST"
Sheet5.Move After:=Sheet1
On Error GoTo 0
End Sub
Now anytime someone clicks the sheet, the sheet will be renamed to its original name and moved to where you want it in the workbook.

If you want to still protect the workbook, you can do this. You would have to add a button in the ribbon, worksheet, or a keyboard shortcut for the user to run it.
Sub newSheet()
Dim sheetName As Variant
sheetName = InputBox("What do you want to name the sheet?")
ActiveWorkbook.Unprotect 'password if needed
ActiveWorkbook.Sheets.Add.Name = sheetName
ActiveWorkbook.Protect Structure:=True, Windows:=False
End Sub

Related

Copy/Paste worksheet to new workbook without that worksheet's VBA code

What I want to do is to create a normal Excel workbook (.xlsx) where I summarize the inputs/outputs of some simulations by copying some sheets from a Macro-enabled workbook (.xlsm) and pasting in a normal Excel workbook. The original sheets have macros, shapes, named ranges, dropdown lists, and some formatting. I want some properties of the sheets to come over to the new workbook (named ranges, row and column formatting, cell formatting) but not others (shapes, dropdown lists, macros). I also have to distribute the .xlsm workbook to other users, so I want a solution that doesn't require the user to grant permissions.
If I copy/paste like below, then I get all the properties of the worksheet to come over to the new workbook. I have figured out how to delete shapes and remove dropdown list formatting from the sheet in the new workbook, but I can't remove the worksheet's VBA code without modifying the VBA references.
If I paste special, then I can avoid bringing over the worksheet's VBA code, but I can't bring over the named ranges.
CopySheetToWB(sht as string, wb_New as workbook)
Dim sht_Name as string, rng As Range, shp as shape
Dim ws As Worksheet, wb As Workbook, ws_New As Worksheet
' set sheet in CURRENT wb
Set wb = ThisWorkbook
Set ws = wb.Worksheets(sht)
' copy/paste sheet to NEW wb
ws.Copy after:=wb_New.Sheets(wb_New.Sheets.Count)
' delete shapes from NEW ws
Set ws_New = wb_New.Worksheets(sht)
For Each shp In ws_New.Shapes
shp.Delete
Next shp
' remove dropdown lists from copied sheet
ws_New.Cells.Validation.Delete
End Sub
The answer to my problem turned out to be quite simple. My old code that wasn't working and my new code that is working. If you save the file type correctly, then the macros in the worksheets are not a problem as they are removed.
wb_New.SaveAs FileName:=str_fName, FileFormat:=xlWorkbookNormal 'old code
wb_New.SaveAs FileName:=str_fName, FileFormat:=51 'new code
Also make sure to include the file extension in str_fName. VBA doesn't automatically append the correct file extension based on the FileFormat you choose.

Naming a Cell in Excel

I'm trying to create a series of macros to audit some financial models.
The first macro I’m trying to create is one that names the current cell. Why? I want to name the cell, after that I’m going to record a macro to click the “Trace Precedents” and go to the cell that has the relationship.
After that I need to go back to the original cell, thats the named one. That's easy on the go function, but I need to the naming macro working
My recorded code for the naming macro is as follows:
Sub Namer ()
ActiveWorkbook.Names.Add Name:="Name1", RefersToR1C1:="=Workings!R42C6"
ActiveWorkbook.Names("Name1").Comment = ""
End Sub
I have the following problems:
I need to name the current cell on a workbook with a lot of sheets. I’m gonna be moving between sheets but my recorded code has a “fixed” sheet.
How can I fix that? Name the current cell on the current sheet
Something like this should help you ...
Public Sub CreatesNames()
Dim objSheet As Worksheet
For Each objSheet In ThisWorkbook.Worksheets
objSheet.Names.Add Name:="Name1", RefersTo:=objSheet.Range("A1")
Next
End Sub
... something to note, names can be created at the worksheet level or at the workbook level, so, if you're going to be creating the same name across worksheets then you need to use a Worksheet object, not the Workbook object.
So to use the active cell ...
Sheet Level
ActiveSheet.Names.Add Name:="Name1", RefersTo:=ActiveCell
Workbook Level
ActiveWorkbook.Names.Add Name:="Name1", RefersTo:=ActiveCell
I hope that helps.

I need an Excel Macro that will automatically select "Yes" for a pop-up window regarding a name that already exists

I've built a Macro that will copy all of the sheets in one workbook and paste them into another workbook. So far it works just fine, but while the macro is in the midst of copying and pasting each sheet, excel flashes a pop up window that reads "The name '(insert any name here)' already exists. Click Yes to use that version of the name, or click No to rename the version of '(insert any name here)' you're moving or copying."
It does this for multiple named items that have the same name in the sheets that I'm copying and pasting to the new workbook, so this window pops up a lot (about 30 times!). Is there a Macro that will automatically select "Yes" so I don't have to keep selecting yes over and over again? I've listed the macro for copying and pasting all worksheets into another workbook of mine below for reference.
Sub Copy_Sheets()
Dim b1 As Workbook, b2 As Workbook
Dim sh As Worksheet
Workbooks.Open Filename:="C:\Users\Documents\Test.xlsx"
Set b1 = ActiveWorkbook
Workbooks.Open Filename:="C:\Users\Experiment.xlsx"
Set b2 = ActiveWorkbook
For Each sh In b2.Sheets
sh.Copy After:=b1.Sheets(b1.Sheets.Count)
Next sh
End Sub
You can insert:
Application.DisplayAlerts = False
before the copying, and
Application.DisplayAlerts = True
after.
This way you'll turn off the alerts before copying and will turn them on after.

Prevent user duplicating or adding new sheets to Excel manually

I am new in using Excel VBA. I have an Excel VBA code that will duplicate a certain sheet for n times. I want to prevent user from changing the workbook structure manually (such as adding new sheet, duplicating sheet, or moving sheet), but I still want the VBA to be able to run.
How should I do it?
I was hoping you could use UserInterfaceOnly but that appears to be only worksheet level and not workbook level - it locks the worksheet for users, but allows VBA code to make updates.
The only other way I can think of is to lock and unlock the workbook as needed:
Sub Test()
Dim wrkSht As Worksheet
UnlockWorkbook
Set wrkSht = ThisWorkbook.Worksheets.Add
LockWorkbook
End Sub
Sub LockWorkbook()
ThisWorkbook.Protect Password:="aaa", Structure:=True
End Sub
Sub UnlockWorkbook()
ThisWorkbook.Unprotect Password:="aaa"
End Sub

Hide sheets in Excel VBA

I am trying to create an Tracker. Need help with these ideas:
When the Excel book is opened, it must show only the "Tracker" Worksheet. All other sheets need to be hidden. Now am using the code {sheet.visible = xlveryhidden} But the code is too long, I have to include each sheet name. I need help with a code to show only the Tracker sheet but to hide all other sheets in the workbook.
I've included two comboboxes as year and month. Also included a Command button OK & Cancel. When clicked on OK, it has to verify the IF condition and show a specific sheet. I have no issues with that. it is showing a specific sheet. But I need a single line code to hide all other sheets
I have also included a "New Tracker" Command button. When Clicked on it, it must include a New sheet. Also too let the user to rename it. This worksheet must also be hidden once saved.
I aware the process is simple, but not sure how to complete it.
To hide all but the Tracker worksheet
Sub hidesheets()
Dim ws As Worksheet, wb As Workbook 'Create the variables we'll use for our worksheet and workbook objects.
Set wb = Excel.ActiveWorkbook 'Set the workbook variable to equal the active workbook. This can also be set to equal a named workbook if you want.
For Each ws In wb.Worksheets 'Loop through each worksheet in the workbook.
If Not ws.Name = "Tracker" Then 'If the worksheet's name isn't "Tracker" then...
ws.Visible = xlSheetVeryHidden 'Set it to very hidden.
Else 'If it is the "Tracker" worksheet, then ...
ws.Visible = xlSheetVisible 'Set it to visible.
End If 'End the IF/THEN statement.
Next ws 'Repeat for the next worksheet in the workbook.
End Sub
You'll have to modify this if you want it to work with a command button or do something with another worksheet. But it should get you started. I'd do it but I don't quite have the time right now.

Resources