Hide Command Buttons & Worksheets

iecalpoly

New Member
Joined
Nov 5, 2009
Messages
5
I cannot figure out why my code will not hide command buttons or worksheets based on a given password. I have attached the code so please help me out.

Code:
Private Sub Workbook_Open()
Dim strUser As String, strPassword As String
strUser = InputBox("Enter User Name")
strPassword = InputBox("Enter Password")
Select Case strUser
Case "O"
If strUserPassord = "O4" Then
ActiveWorkbook.Sheets("Main").Select
O.Enabled = True
GOODBYE.Enabled = True
S.Enabled = False
R.Enabled = False
A1.Enabled = False
A3.Enabled = False
A4.Enabled = False
Sheets("Set4").Visible = True
Sheets("Set5").Visible = False
Sheets("Set61").Visible = False
Sheets("Set63").Visible = False
Sheets("Set64").Visible = False
Sheets("Set7").Visible = False
Sheets("File").Visible = False
Sheets("Fild").Visible = True
Sheets("Ts").Visible = True
Sheets("26d").Visible = False
Sheets("263d").Visible = False
End If
Case "S"
If strUserPassord = "O5" Then
ActiveWorkbook.Sheets("Main").Select
O.Enabled = False
GOODBYE.Enabled = True
S.Enabled = True
R.Enabled = False
A1.Enable = False
A3.Enabled = False
A4.Enabled = False
Sheets("Set4").Visible = False
Sheets("Set5").Visible = True
Sheets("Set61").Visible = False
Sheets("Set63").Visible = False
Sheets("Set64").Visible = False
Sheets("Set7").Visible = False
Sheets("File").Visible = False
Sheets("Fild").Visible = True
Sheets("Ts").Visible = True
Sheets("261d").Visible = False
Sheets("263d").Visible = False
End If
Case "A"
If strUserPassord = "O6" Then
ActiveWorkbook.Sheets("Main").Select
O.Enabled = False
GOODBYE.Enabled = True
S.Enabled = False
R.Enabled = False
A1.Enable = True
A3.Enabled = True
A4.Enabled = True
Sheets("Set4").Visible = False
Sheets("Set5").Visible = False
Sheets("Set61").Visible = True
Sheets("Set63").Visible = True
Sheets("Set64").Visible = True
Sheets("Set7").Visible = False
Sheets("File").Visible = False
Sheets("Fild").Visible = False
Sheets("Ts").Visible = True
Sheets("261d").Visible = True
Sheets("263d").Visible = True
End If
Case "R"
If strUserPassord = "O7" Then
ActiveWorkbook.Sheets("Main").Select
O.Enabled = False
GOODBYE.Enabled = True
S.Enabled = False
R.Enabled = True
A1.Enable = False
A3.Enabled = False
A4.Enabled = False
Sheets("Set4").Visible = False
Sheets("Set5").Visible = False
Sheets("Set61").Visible = False
Sheets("Set63").Visible = False
Sheets("Set64").Visible = False
Sheets("Set7").Visible = True
Sheets("File").Visible = False
Sheets("Fild").Visible = True
Sheets("Ts").Visible = True
Sheets("261d").Visible = False
Sheets("263d").Visible = False
End If
 
Case Else
MsgBox "Incorrect password or user name", vbCritical, Password
Application.Quit
End Select
End Sub
 
Last edited by a moderator:
I created a command button from the Forms toolbar, Button 1.

I then used this code to hide the button.
Code:
Dim cmd As Object
    Set cmd = Worksheets("Sheet1").Shapes("Button 1")
    
    cmd.OLEFormat.Object.Visible = False
I also tried this code which 'toggles' the visibility of the command button.
Code:
Dim cmd As Object
    Set cmd = Worksheets("Sheet1").Shapes("Button 1")
    
    cmd.OLEFormat.Object.Visible = Not cmd.OLEFormat.Object.Visible
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,214,540
Messages
6,120,107
Members
448,945
Latest member
Vmanchoppy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top