Hide or disable a button if the sheet is protected - excel

For my form I need to hide or disable a textbox with a on-click delete macro attached to it when the sheet is protected. I'm talking about excel's build-in protection system. I've looked at several tutorials but I can't seem to get it to work properly.
I tried multiple things including this:
If ActiveSheet.ProtectContents = True Then
TextBox1.Visible = False
Else
TextBox1.Visible = True
End If
Any idea how to do this?

Change
If ActiveSheet.ProtectContents = True Then
TextBox1.Visible = False
Else
TextBox1.Visible = True
End If
To
If ActiveSheet.ProtectContents = True Then
ActiveSheet.TextBox1.Visible = False
Else
ActiveSheet.TextBox1.Visible = True
End If
You are not declaring where the textbox is. This will fix it

It is not clear whether the textbox is embedded in the worksheet or in a userform.
If in the worksheet is it a text box based on a shape? These are created from the INSERT tab on the Ribbon.
Or is this an ActiveX textbox? These are created from the DEVELOPER tab on the Ribbon.
If on the other hand, you are referring to a textbox on a userform then you can use the following code (assuming the userform code name is UserForm1):
UserForm1.Controls("NameOfYourTextBox").Visible = Not ActiveSheet.ProtectContents

Related

UserForm ListBox Change not Updating TextBox Visibility

I have a UserForm that has a ListBox named EIDLList and after that there's a TextBox EIDLAmountLabel and EIDLAmountText. I've written a snippet of code that is supposed to grey out both TextBoxes if the ListBox selection is set to "No" but for some reason it's not working. I've tried about 10 different variations using Case, If, UCase, etc and none work. I appreciate any input.
Private Sub EIDLList_Change()
If EIDLList.Value = "No" Then
EIDLAmountLabel.Enabled = False
EIDLAmountText.Enabled = False
Else
EIDLAmountLabel.Enabled = True
EIDLAmountText.Enabled = True
End If
End Sub
I changed the fields from ListBox to ComboBox and now the code works perfectly. Not sure how or why that matters.

Disable specific sheet ("ply") right click option using Excel VBA

I'm trying to disable (or hide) specific right click options on the Excel sheet/tab menu. I don't want to disable the entire menu per this thread.
I've tried in both Excel 2010 & 2016, same result. The code runs fine but the options in the sheet right click menu are still enabled. I've tried using the control's name and ID to no avail.
Private Sub Worksheet_Activate()
Application.CommandBars("cell").Controls("cut").Enabled = False 'Works
With Application.CommandBars("Ply")
.Controls("Insert...").Enabled = False
.Controls("&Select All Sheets").Enabled = False
.Controls("Select All Sheets").Enabled = False
.FindControl(ID:=946).Enabled = False '&Select All Sheets ID:946
'.Enabled = False 'Works but don't want entire menu disabled
End With
End Sub
After code, options are still enabled:
Thanks in advance.

Disable/Enable Tabstrip on Multitab based on Checkbox value- EXCEL

I have a userform with a multitab containing 8 tabs. I require a user to use checkboxes on tab 6. I'm try to disable the user from selecting the next tab (tab 7) if Checkbox 1 and Checkbox 2 are left unchecked. They should only be able to access the next tab if they select one of the two checkboxes.
Private Sub MultiPage1_Change()
If Failed1.Value = False Or Passed1.Value = False Then
Me.MultiPage1.Pages(7).Enabled = False
Exit Sub
ElseIf Failed1.Value = True Or Passed1.Value = True Then
Me.MultiPage1.Pages(7).Enabled = True
End If
End Sub
I can't quite get the code to work the way I want it to. Any help would be greatly appreciated.
FYI - I'm a VBA n00b.
Me.MultiPageName.Pages(index).enabled = false
Me = the form active where multipage is present
MulitipageName = named multipage
Index = place of the page to enable copied from:http://www.excelforum.com/excel-programming-vba-macros/534056-is-it-possible-to-disable-a-tab-page-on-a-multipage-userform.html

Disable button using Excel VBA

I want to disable a button with VBA code like this:
ActiveSheet.Shapes("Button 1").ControlFormat.Enabled = False
I tried:
Set b1 = ActiveSheet.Buttons("Button 1")
b1.Enabled = False
And:
Me.Shapes("Button 1").ControlFormat.Enabled = False
My button name is correct, because it doesn't give me an error message, so the code is completely run through.
After this script I can click on that button and the assigned macro runs. Nothing should happen when I click on it.
Disabling a Form button (not talking ActiveX here) does not prevent the assigned macro to run and does not gray out the button. The code below does exactly that based on the version got from Excel. If you did not assign a name to your Form button, you can also use (Buttons(1).
If Excel version = 16 or higher the button is "enabled" by making it black and assigning my macro, else the button is "disabled" by making it gray and assigning no action to it.
Code can e.g. reside in Private Sub Worksheet_Activate() within sheet "Test Sheet"
If Application.Version < 16 Then
Worksheets("Test Sheet").Buttons("button_name").Font.Color = 8421504
Worksheets("Test Sheet").Buttons("button_name").OnAction = ""
Else
Worksheets("Test Sheet").Buttons("button_name").Font.Color = 0
Worksheets("Test Sheet").Buttons("button_name").OnAction = "'Name of the workbook.xlsm'!my_macro_name"
End If
Probably you are using ActiveX Button. Try this:
Sheets("Sheet1").CommandButton1.Enabled = False '--->change sheet name as required
EDIT:
______________________________________________________________________________
For a Form control Button the following line
ActiveSheet.Shapes("Button 1").ControlFormat.Enabled = False
disables the button i.e. click event will no longer work but the appearance of the button does not change which gives an impression that the button is still active. So work around for that is to change the color of the text of the button as follows:
Sub disable_button_2()
Dim myshape As Shape: Set myshape = ThisWorkbook.Worksheets("Sheet1").Shapes("Button 2")
With myshape
.ControlFormat.Enabled = False '---> Disable the button
.TextFrame.Characters.Font.ColorIndex = 15 '---> Grey out button label
End With
End Sub
And to bring back button to its original state write:
Sub activate_button_2()
Dim myshape As Shape: Set myshape = ThisWorkbook.Worksheets("Sheet1").Shapes("Button 2")
With myshape
.ControlFormat.Enabled = True '---> Enable the button
.TextFrame.Characters.Font.ColorIndex = 1 '---> Highlight button label
End With
End Sub
I suggest to create a shadow button/shape with exactly same size/position, but different color (fill and/or text to your liking) and no macro/action attached. Then just change the .visible property of your primary shape. Visible = button is active; not visible button is e.g. grayed out and has no action/is passive.
Only tested on Excel 2016 x86
I continued to receive errors utilizing .ControlFormat. solutions.
After much searching I found another solution that worked great for my needs of disabling Shapes/Buttons.
To mimic the .Enabled property of a uf control, you might toggle the .OnAction property of a shape.
Function ShapeIsEnabled(aShape As Shape) As Boolean
ShapeIsEnabled = (aShape.OnAction <> "")
End Function
Sub EnableShapeMacro(aShape As Shape)
aShape.OnAction = aShape.AlternativeText
End Sub
Sub DisableShapeMacro(aShape As Shape)
aShape.AlternativeText = aShape.OnAction
aShape.OnAction = vbNullString
End Sub
Note the use of the .AlternativeText property to store the macro name.
source: mikerickson
https://www.excelforum.com/excel-programming-vba-macros/1267897-disable-action-of-macro-enabled-shape.html#post5080833

Hiding the formula bar in Excel for a specific file

I would like to hide the formula bar in a specific Excel file and only in this file. I know we can do it with VBA (Application.DisplayFormulaBar = False) but I am wondering if there is another solution like editing the CustomUI file.
Usually I edit the CustomUI file for hiding ribbon, adding custom tabs, ... It would be nice if we can hide the formula bar in this way.
Any suggestions?
Thanks.
Short answer is: No, you cannot.
Unfortunately, you cannot hide it by editing the CustomUI file. The formula bar has to be hidden using VBA. That being said, you can just run the hide operation on the Workbook_open event.
Private Sub Workbook_Open()
Application.DisplayFormulaBar = False
End Sub
You can turn it on/off depending on the active sheet like so:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "Sheet1" Then
Application.DisplayFormulaBar = False
Else
Application.DisplayFormulaBar = True
End If
End Sub
I know it's a 2011 issue, but the solutions shown above don't seem to be the right ones for the problem in question.
If you modify using vba Aplication.whatelse this change applies to the entire excel app
To fix this instead of turning on/off when you open/close the document uses the sheet's activate/deactivate events
This way when your hidden document is turned on and when you turn off swatches.
This works perfect for me, to hide status bar and formula bar.
I leave you code to see how it works
Private Sub Workbook_Activate()
Application.DisplayStatusBar = False
Application.DisplayFormulaBar = False
End Sub
Private Sub Workbook_Deactivate()
Application.DisplayStatusBar = True
Application.DisplayFormulaBar = True
End Sub
You can accomplish this by using the workbook activate en deactivate events.
Just put Application.DisplayFormulaBar = False into the activate event and Application.DisplayFormulaBar = true in the deactivate event.
To avoid all opened Excel sheet formula bar hidden you can go for hiding the formula for a particular excel.
Sub test()
Sheet1.Unprotect "test"
Range("A1").FormulaHidden = True ' range which you want to hide formula.
'your code here
Sheet1.Protect "test"
End Sub

Resources