How to Protect the sheet name not the entire sheet? - excel

I need to protect the sheet name by preventing
any change to the sheet name, or
the sheet being deleted.
This must be done without protecting the entire sheet using the Protect Sheet or Protect Workbook options.
Is there any way to do this with VBA?

Right click the sheet tab that you wish to protect
View Code
Copy and paste in the code below
This code disables the delete control on the sheet (but not right click on cell) menu when the sheet is activated. The control is enabled when the sheet is de-activated
The code will also name the sheet "NameOfSheet" when the sheet is de-activated. This is a workaround to prevent the sheet being renamed
Private Sub Worksheet_Activate()
Application.CommandBars.FindControl(ID:=847).Enabled = False
End Sub
Private Sub Worksheet_Deactivate()
Application.CommandBars.FindControl(ID:=847).Enabled = True
Me.Name = "NameOfSheet"
End Sub

I don't think you can. What you can do, you can make a worksheet a very hidden one (accessible only from VBA) and in case of the deleted sheet, you can copy it and make a copy visible.

Would this approach work?
Select all cells in the sheet, then UN-lock all cells with "Lock
Cells" (yellow background of padlock turns white).
Write the name of the sheet in a (fixed or named) cell, then lock
this cell ONLY ("Lock Cells", padlock background turns yellow).
Then Protect workbook, but allow every action, except the first one
"Select Locked Cells".
The user can do everything except selecting the cell with the sheetname (and delete rows/columns).
Now write a VBA to compare the actual sheetname with the data in the protected named cell (or fixed reference e.g. A1).
Run this script either on every change (probably too much) or at least on close of
the workbook.
As long as the sheetname is always in the same cell (e.g. A1), you can then loop through all sheets, compare their name with the data in cell A1 and correct the sheet name if required.

Related

Hide or unhide Excel sheet but formulas should work

I have used this method before but after six months I cannot.
I created data entry fields through which I move data from Sheet1 to Sheet2 using a button.
I do not want users to see Sheet2 so that they cannot change anything. I was using the formulas below.
Sheets("Data Sheet").Visible = True
Sheets("Data Sheet").Visible = False
After hiding the sheet I protect them with the password. The main advantage of code is that I can still post the data from Sheet1 to Sheet2 after hiding.
Upon research I found one more formula to hide the sheet but after hiding sheet2 posting via Sheet1 to Sheet2 (which is hidden) gives an error.
ThisWorkbook.Worksheets(Array("Data Sheet")).Visible = xlSheetHidden
How can I hide Sheet2 and continue posting the entries via Sheet1.
You can manipulate data on a hidden sheet, no need to unhide it.
Unless you are trying to select cells, but you shouldn't have to do that.
You do however need to unlock a sheet before editing it if the cells are protected. You can do this as part of the process.
Sheets("Data Sheet").Unprotect Password:="pass"
----
whatever code you have
----
Sheets("Data Sheet").Protect Password:="pass"
You do might want to have some error handling in there however, since if the code throws an error, the sheet will be left unlocked.
a) The statements Sheets("Data Sheet").Visible = False and ThisWorkbook.Worksheets(Array("Data Sheet")).Visible = xlSheetHidden do basically the same. However, you will need to understand why it is important to qualify the Sheets: ThisWorkbook.Worksheets will access the worksheets from the workbook where the code lives in, just writing Sheets (or Worksheets) will access the sheets of the ActiveWorkbook - and that may be a different workbook. The Array("Data Sheet")-part is unnecessary in your case (you can pass an array of sheet names to hide more than a sheet at once). Setting visibility to xlSheetHidden or to False is identical.
b) Hiding a sheet and protecting a sheet are two different, independent things. You can protect a sheet but not hide it, you can hide a sheet but let it unprotected.
c) The main idea of protecting a sheet is to allow user input only at specific cells. The user can change the content of the sheet, but only to cells which are not formatted as "Locked".
d) If you protect a sheet via Excel (no matter if hidden or not) and want to modify something via code, you will need to unprotect it (and protect it again after the code is done). However, when protecting the sheet via code, you can specify that you want to allow the code to make modifications by setting the UserInterfaceOnly-parameter:
Thisworkbook.Sheets("Data Sheet").Protect Password = "IWontTellYou", UserInterfaceOnly:=True
e) If you never want to show the sheet, set the visibility not to hidden, but to veryHidden. With that, the sheet cannot be made visible by the use from within Excel: It will not be listed under "Unhide..." - in that case there is not need to protect it.
Thisworkbook.Sheets("Data Sheet").Visible = xlSheetVeryHidden
(Note that in that case you can make the sheet visible again only via code, but a one-liner in the immediate window is sufficient)
You will have to write a code to first unprotect sheet2, then paste the data and protect sheet2 again.

Read One Cell and Copy to Another Only Upon Opening Spreadsheet

I need to copy the data in cell c3 to cell i11 when I open my spreadsheet. I do not want i11 to change after this action until I reopen the spreadsheet.
You can set the code in the ThisWorkbook part of your projects. Select Workbook and Open or use the following code.
The working formula is the value you see me do below, but this can be achieved a number of way such as using Cells(3,3).Value. Additionally the way the sheet is referenced can vary. I put both the Activesheet reference and a explicit sheet reference, depending how your sheet is structured you may have to use one over the other, but choose one below and see how your sheet handles it and if adjustments are needed.
Private Sub Workbook_Open()
'Use one of these depending on how your sheet opens
ActiveSheet.Range("I11").Value = ActiveSheet.Range("C3").Value
Worksheet("YourWorksheetHere").Range("I11").Value = Worksheet("YourWorksheetHere").Range("C3").Value
End Sub

Drop-down list of hyperlinks to different sheets in Excel

I have 5 different sheets: S1, S2, S3, S4, S5. And an "Info" sheet where i have the overall information, where in every row I want a dropdown along with direct link to one of the sheets(S1, S2, S3, S4, S5). I tried using Indirect function along with Address but it is not dynamic. Is there a way of how to make a drop-down list with hyperlink to the sheet directly inbuilt with that?
I hope i could describe my situation.
No need to re-invent the wheel. This is already built into Excel. Righ-click the sheet navigation arrows in the bottom left hand corner and the list of sheets will pop up. Click the sheet you want to go to. Hidden sheets (like Sheet3 in my screenshot) will no be listed.
Place a drop-down in cell, say, A5.
In B5 enter:
=HYPERLINK("#" & A5 & "!A1",A5)
This will create a "hot" hyperlink to cell A1 of the sheet you picked in the drop-down:
EDIT#1:
Place your drop-downs in column A. Each drop-down can select any worksheet. Then in the worksheet code area enter:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, sht As String, sh As Worksheet
Set A = Range("A:A")
If Intersect(Target, A) Is Nothing Then Exit Sub
sht = Target.Value
For Each sh In Sheets
If sh.Name = sht Then
sh.Activate
End If
Next sh
End Sub
whenever you change a value in column A, the code checks it the new value is a valid sheet-name. If it is, the code jumps to that sheet. If it is not a valid name, nothing bad happens.
This approach has the advantage that if you add/remove worksheets, no code change is required.
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!

Excel graphical glitches when moving between sheets with private subs?

I encountered an odd graphical glitch when running private subs on a sheet then using a macro to jump to another sheet with a private sub on it. Basically excel is getting slowed down (the drop down menu's in the ribbons get messed up as well).
IE:
Sheet 1 has
Private Sub Worksheet_Deactivate()
Sheets("Sheet1").Visible = False
End Sub
Sheet 2 has the above code as well except Sheet2 would be the one made hidden when deactivating the worksheet.
With a button placed on sheet1 which trigger the following macro
Sub Sheet1_Button1_Click()
Sheets("Sheet2").Visible = True
Sheets("Sheet2").Select
End Sub
For testing purposes I was just using another macro assigned button on sheet2 that jumped back to sheet one and found that caused the issue. Does anyone know what's going on here and how to prevent it? Maybe this is more of a Microsoft issue?
In my original workbook I had a private sub on a "Cost Estimations" sheet that would run some some code to un-hide used lines and re-hide unused lines in a table that was referencing another sheet. Then I had a macro assigned button on that same sheet that would open a normally hidden sheet with some more info on it. The "hidden" sheet had a private sub on it that automatically hide it when the user clicks off of the sheet just like the "Sheet1" in my example. Additionally in the original workbook it was causing all the information from "cost estimations" to display on the "hidden" sheet, but only if calculations were set to automatic. I was however unable to replicate that in my test worksheet.

Excel 2010 - Multiple Types of Locks on the Same Sheet

So I'm trying to create an excel sheet that handles a user input/output task (I know that this is a kludge at best and offensive at worst). So I want the user to be able to edit the input cells (and only the input cells), and select (but not edit) the outputs.
Example: Grid A1:C3. A1:A3 are completely locked - unselectable. B1:B3 are selectable and editable. C1:C3 are selectable and uneditable.
Is this possible? My gut is telling me no, but I thought I'd ask anyway.
It is possible using worksheet protection:
In the Format Cells dialog of the cells B1:B3, uncheck the "Protected" checkbox in the last tab (Protection). Then protect the worksheet (right click the worksheet tab at the bottom->Protect Sheet). Once the sheet is protected, the user can only edit those cells that are unprotected.
Regarding the selection of C1:C3 and non-selection of A1:A3 - you can allow/prevent the user from selecting protect cells in the same sheet protection dialog. However, this is a sheet wide setting, so be default you can only fully prevent selecting all protect cells or allow selection of all cells.
If you only want prevent selection of A1:A3 because you don't want the user to see the formula, just check "Hidden" in the Format cells dialog - this way the user does not see the formula.
If you really need to separate between selectable and unselectable, either split across two worksheets - or use a little VBA macro. To do so, open the VBA editor (Alt+F11) and double click your worksheet in the top-left list. In the code window, enter the following code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Me.ProtectContents And Intersect(Target, Range("B1:C3")) Is Nothing Then Range("B1").Select
End Sub
This way, every time a cell outside your required range is selected, B1 is selected instead

Resources