Change tab color when sheet is protected - excel

Is there a way to have the tab color change depending on whether a sheet is protected or not? Eg. protected = green, unprotected = red.
I'm trying to find an easy way to visually spot which sheets are protected. I have multiple sheets, some I need protected and some I need unprotected. I edit the protected sheets on a daily basis but I need to protect them when saving so other users don't accidentally edit the data.

Using what #BruceWayne said. You just need to loop through all your worksheets and check if they're protected. The below code will loop through all then color Green if it's protected or Red if it's unprotected. You can change the event to trigger on whatever action you need. Be sure to put this code IN the workbook and not a module.
Private Sub Workbook_Open()
Dim wbk As Workbook
Dim ws As Worksheet
Set wbk = ThisWorkbook
For Each ws In wbk.Worksheets
If ws.ProtectContents = True Then
ws.Tab.Color = vbGreen
Else
ws.Tab.Color = vbRed
End If
Next
End Sub

Related

How do I amend this VBA so it runs when a specific worksheet is selected?

I have this VBA written. Essentially what it does is; I've used 'Define Range' ("Test") on a few cells in a worksheet. The VBA makes sure that whenever the workbook is opened, it automatically zooms in to fit that range. Right now, I think it only triggers when the workbook is opened, and it gives an error message if that worksheet is not selected when the workbook is opened. Now, I would like it to run only if the specific worksheet is selected. I've literally copied it from someone else so don't really know how I would amend this...
Thanks in advance!
Private Sub Workbook_Open()
Range("Test").Select
ActiveWindow.Zoom = True
Cells(1, 1).Select
End Sub
Zoom & Scroll To Cell When Worksheet Is Activated...
... and if the worksheet is active when the workbook is opened.
It is assumed that the range is of Workbook scope.
ThisWorkbook Module
Option Explicit
Private Sub Workbook_Open()
Const WorksheetName As String = "Sheet1"
If ActiveSheet.Name = WorksheetName Then SelectMyRange
End Sub
Sheet Module, e.g. Sheet1...
... where Sheet1 is the code name i.e. the name not in parentheses, e.g. Sheet1(Data), seen in the VBE Project Explorer window.
Option Explicit
Private Sub Worksheet_Activate()
SelectMyRange
End Sub
Standard Module, e.g. Module1...
... created by using Insert Module.
Option Explicit
Sub SelectMyRange()
Const RangeName As String = "Test"
With Range(RangeName)
.Select
ActiveWindow.Zoom = True
Application.Goto .Cells(1), True
End With
End Sub

Unprotect sheet in MS Excel for ActiveX controls

I implemented smart dropdown (using Active X controls) in my sheet, so it is protected.
This functionality is not able to work because of protection.
When I right click on the sheet and Unprotect it, providing the password, it works.
I do not want the user to do this manually. I want that when user opens the sheet the sheet should get unprotected using VBA code.
The entire cells in the sheet are Locked (Format cells-> Protection -> Locked marked).
I cannot untick the Locked mark, as there are some validations which gets failed and not allow me to upload such template.
Below is an example as I cannot share the application code.
VBA code:
In sheet1-> name of sheet is "tab0"
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, _
Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Unprotect Password:="xxx"
Inside ThisWorkbook I have this
Private Sub Workbook_Open()
ThisWorkbook.UnProtect Password = "password"
End Sub
A late input to this question:
I've been experiencing similar problems with Unprotecting sheets, and ended up doing aa fair bit of testing to find what works and what doesn't (always!).
The problem appears to be when assigning the sheet to an object and using the object's dot notation to unprotect e,g.:
'Setting the worksheet (globally) in sub Main()
Public goXsTT As Worksheet
Set goXsTT = goXbWb.Sheets(2)
'Somewhere else in the code
goXsTT.Unprotect
'This ***may*** fail, whereas:
Sheets(2).Unprotect
'Works!
No idea why this is the case - just pointing out that it's a workaround if you're stumped, as I was. Hope it helps. DG

How to lock specific range in worksheet but don't lock sheet

I want to protect only range D1:D9 in my worksheet to prevent users from editing. However, I do not want to protect the whole sheet. Is there a way to go about this? I've been reading through posts and trying to implement their codes etc, but none of them seem to work for me. Your help is greatly appreciated!!
Private Sub Workbook_Open()
Dim Sheet1 As Worksheet
Set Sheet1 = Sheets("Screening Request")
Sheet1.Cells.Locked = False
Sheet1.Range("D1:D9").Locked = True
' set the value
Sheet1.Protect
End Sub
Unlock all cells:
ws.Cells.Locked = False
Lock yours
ws.Range("D1:D9").Locked = True
Protect the specific sheet.
Dim ws As Worksheet
' set the value
ws.Protect
The following code works as expected and can be run multiple times if you ever want to change the locked range:
Private Sub Workbook_Open()
Dim MySh As Worksheet
Set MySh = Sheets("Screening Request")
MySh.Unprotect
MySh.Cells.Locked = False
MySh.Range("D1:D9").Locked = True
MySh.Protect
End Sub
It unprotects the sheet so it wont throw an error from the last time you protected it. Then unlocks all cells, so only the specified range is locked and nothing else. Then it locks the specified range. Then it re-protects the sheet.
Edit As per psyduck's comment merged cells are an issue when working with ranges. In this case two columns are fully merged so the range can be altered to D1:F9 in cases that it's not the entire range which is merged do separate instances like so:
MySh.Range("D1:D8").Locked = True
MySh.Range("D9:F9").Locked = True
Thanks to Mathieu Guindon for the suggestion.

How to trigger enter keypress of all textboxes in multiple sheets? - VBA

My workbook has three sheets(named Sheet1 ~ Sheet3) with TEXTBOXES.
This has a module.
Public dontDoThat As Boolean ' a public variable, visible throughout all your project you'll use to give way to synchronizing activity
Option Explicit
Sub Synchronize(txt As String, shtName As String)
dontDoThat = True ' set your public variable to True and prevent subsequent TextBox1_Change() events to run it again
Dim sht As Variant
For Each sht In Array("Sheet1", "Sheet2", "Sheet3")
If sht <> shtName Then Worksheets(sht).TextBox1.Text = txt
Next
dontDoThat = False ' set your public variable to False and allow subsequent TextBox1_Change() events to run it
End Sub
These code can synchronize the TEXTBOX on all sheets.
But It's only for Text.
If I type some text in TEXTBOX1 of Sheet1, the same text will be display in TEXTBOX1 of all other sheets.
but The search function does not work on other sheets.
After I typed some text on TEXTBOX1 of Sheet1 and When I press the enter key, The search function works only in Sheet1.
I want to trigger the enter keypress on Textboxes of all Sheets.
And these sheets also have TEXTBOX2.
Thus I want to know how to apply syncronization to TEXTBOX1 and TEXTBOX2 as well.
I need someone help.
Neo, I've done something like this before - where you create an autofilter either with Change event, or a specified KeyDown event (usually the Enter key - vbKeyReturn). Since you're using a KeyDown event, it wouldn't be overly exhaustive to use collections. You can, say, roll all of your TextBoxes in a collection either by name similarities, or by TypeName.
Specifically, for why I'm thinking you're having issues, perhaps loop through the Worksheets collection with an actual Worksheet object, and not a variant. And, since they're Worksheets objects, you can use OLEObjects.
Dim sht As WorkSheet
Dim x as Integer
x = 1
For Each sht In ThisWorkbook.WorkSheets
Do Until sht.OLEObjects("TextBox & x") is Nothing
If sht.Name <> shtName Then sht.OLEObjects("TextBox & x").Object.Text = txt
x = x + 1
Loop
Next
And even within that loop, you can do another loop to iterate over your multiple text boxes. Let us know how you want to proceed. Hope this helps...

Locking cells with VBA

I have a spreadsheet with several sheets, which is protected except where I want people to make changes, and all is password protected. I am trying to make a command button so others can view the data, but can't make changes to the cells. Here is what I have (not working quite right).
Private Sub mdRead_Click()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
'To open wookbook as read only, while proctecting changes.
Worksheet.Unprotect = True
Worksheet.Range("C10:I23,L10:R23,C25:I36,L25:R36,C45:I58,L45:I58,C60:I71,L60:R71").Select
Selection.Locked = True
Next ws
End Sub
Having set the Locked property, you might want to protect the sheet again.

Resources