Hide multiple worksheets and diplay only one worksheet vba - excel

I am trying to secure my workbook, i have multiple sheets that i need to hide and leave only one sheet to be displayed that will have a command button (Picture 1) when I click on it I have a userform that pops up (Picture 2) with username and password to open specific sheets( ive set different usernames and passwords to open specific sheet)
enter image description here
enter image description here
I wrote this code :
Private Sub CommandButton1_Click()
Dim User, Pass As String
User = Me.TextBox1.Text
Pass = Me.TextBox2.Text
If User = "Admin" And Pass = "123" Then
MsgBox ("Bienvenu")
Application.Visible = True
ActiveWorkbook.Unprotect Password:="password"
Sheets("ACCEUIL").Visible = True
Sheets("Liste Personnes").Visible = True
Sheets("Liste IT").Visible = True
Sheets("Liste PE").Visible = True
Sheets("Liste EM").Visible = True
Sheets("Liste ELC").Visible = True
Sheets("Liste Habilitation").Visible = True
Sheets("Liste EPC").Visible = True
Sheets("Liste ECH").Visible = True
ActiveWorkbook.Protect Password:="password"
Unload Me
Else
'Pole Essais
If User = "Admin1" And Pass = "456" Then
MsgBox " Bienvenue"
Application.Visible = True
ActiveWorkbook.Unprotect Password:="password"
Sheets("Liste Personnes").Visible = True
Sheets("Liste IT").Visible = True
Sheets("Liste PE").Visible = True
Sheets("Liste EM").Visible = True
Sheets("Liste ELC").Visible = True
Sheets("Liste Habilitation").Visible = True
Sheets("Liste EPC").Visible = True
Sheets("Liste ECH").Visible = True
Sheets("PICHON Franck").Visible = True
Sheets("MAGNIER Jean-Fran?ois").Visible = True
Sheets("LAPIERRE Louis").Visible = True
Sheets("HOSSAERT Didier").Visible = True
Sheets("DEBEYER Nicolas").Visible = True
Sheets("GARCIA Manuel").Visible = True
Sheets("GIRARD Sunny").Visible = True
Sheets("SICOT Thimot?e").Visible = True
Sheets("BEAUVILLAIN Maxime").Visible = True
Sheets("WATTEZ Eric").Visible = True
Sheets("PROUVOST Thomas").Visible = True
Sheets("PROUVOST Mathieu").Visible = True
Sheets("GARCIA Manuel").Visible = True
ActiveWorkbook.Protect Password:="password"
Unload Me
Else
MsgBox " Verifier le nom d'utilisateur ou le mot de passe"
End If
End If
End Sub
Private Sub CommandButton2_Click()
Unload Me
ActiveWorkbook.Close True
End Sub
`
it doesnt work as I want the problem is when I close my workbook and reopen it I have the recent sheets That ive been working on displayed on my workbook, and I want the worksheet "ACCEUIL" to be displayed not those Ive been working on the last time I opened the workbook.

thanks #karma it works very well, but I have something to ask, when i open my workbook i have a userform that pops up to edit/ read my database content when I finish working on my database and close the userform (UF_Choix_Service) i have all the worksheets visible again :/ but when im about to close the file I see in the background that the sheets have disappeared so your code is working good i just want to know how to hide the sheets after closing the userform.
My code to display the userfom is : (which I placed in a workbook module)
Sub Workbook_Open()
Gestion_Comp
end sub
where
Sub_Gestion_Comp ()
Application.ScreenUpdating = False
Nom_Classeur = ThisWorkbook.Name
mode_edition = False
UF_Choix_Service.Show
Select Case MsgBox("Voulez vous sauvegarder le fichier ?", vbYesNo + vbQuestion, "Sauvegarde du fichier")
Case vbYes
ThisWorkbook.Save
End Select
End Sub
enter image description here

Please have a look at this code,
maybe you can implement it to your needs.
Sub test()
Dim arr() As Variant
'get all sheet name except "Sheet1" to array
For Each sh In Worksheets
If sh.Name = "Sheet1" Then
Else
ReDim Preserve arr(X)
arr(X) = sh.Name
X = X + 1
End If
Next
'hide all the sheet name in that array, so the only not hidden is Sheet1
For Each sh In arr
If Sheets(sh).Visible = True Then Sheets(sh).Visible = False
Next
'show all the sheet name in that array
For Each sh In arr
If Sheets(sh).Visible = False Then Sheets(sh).Visible = True
Next
End Sub
I want the worksheet "ACCEUIL" to be displayed not those Ive been
working on the last time I opened the workbook.
Put your macro on the workbook module, something like this :
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim arr() As Variant
For Each sh In Worksheets
If sh.Name = "ACCEUIL" Then
Else
ReDim Preserve arr(X)
arr(X) = sh.Name
X = X + 1
End If
Next
For Each sh In arr
If Sheets(sh).Visible = True Then Sheets(sh).Visible = False
Next
End Sub
You need to save the workbook before you close it.
That's if I'm not mistaken what you want.
EDIT:
when I finish working on my database and close the userform
(UF_Choix_Service). I just want to know how to hide the sheets after closing the userform
If you want all the sheets hidden but the ACCEUIL sheet, then on the Sub where you have the line : Unload UF_Choix_Service try to put this line :
Unload UF_Choix_Service '---> this is your existing code to close the userform
For Each sh In Worksheets
If sh.Name = "ACCEUIL" Then
else
If Sheets(sh.Name).Visible = True Then Sheets(sh.Name).Visible = False
end if
Next
end sub
After the code unload/close the userform, the code above say :
Loop through all the existing worksheets,
If the sheet name is "ACCEUIL", do nothing
other than ACCEUIL,
if this other sheet is visible then hide it.
Don't forget to remove the sub I ask you to put in the workbook module (BeforeClose).

Related

VBA Forms in custom addin

I am trying to create an excel addin which has a button when clicked will display a VBA form. Its quite simple one list box and one command button.
Below is the code in Command button
Private Sub CommandButton1_Click()
ThisWorkbook.IsAddin = False
On Error GoTo ErrHandler:
KeyAcc = WorksheetFunction.VLookup(ComboBox1.Value, Sheet1.Range("A:B"), 2, False)
MsgBox KeyAcc
ThisWorkbook.IsAddin = True
Unload Me
Exit Sub
ErrHandler:
MsgBox ComboBox1.Value & " Not found in the Database"
ThisWorkbook.IsAddin = True
Unload Me
ActiveWorkbook.Save = False
End Sub
Code in form load
Private Sub UserForm_Activate()
Application.ScreenUpdating = False
Dim cCount As Integer
ThisWorkbook.IsAddin = False
ThisWorkbook.Sheets("Sheet1").Select
For cCount = 1 To 320
UserForm1.ComboBox1.AddItem Range("A" & cCount).Value
Next
ThisWorkbook.IsAddin = True
ComboBox1.SetFocus
End Sub
The problem i face is whenever the user activates this button on the first book ie, after opening a new excel and performs the operation it works, once done when i try to close the blank workbook it asks do you want to Save your changes to the Addin
Is there any way to avoid this?
You don't need all that work just to load your combobox:
Private Sub UserForm_Activate()
Me.ComboBox1.List = ThisWorkbook.Sheets("Sheet1").Range("A1:A320").Value
ComboBox1.SetFocus
End Sub

Excel macro is printing all sheets and not just selected one

I have an excel macro that is launched by clicking a button.
What macro should do is print out one excel worksheet and increment value in one cell after each print.
Everything works fine EXCEPT the macro ALSO PRINTS the sheet where macro is launched (eventhough that sheet is not selected in code..)
Here is my macro code:
Sub Painike_Napsauta()
Dim i As Long
If MsgBox("Tulosta?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
Cancel = True
Application.EnableEvents = False
Application.Dialogs(xlDialogPrint).Show
Sheets("Lappu").Range("C1").Value = Sheets("Tulosta").Range("C2").Value
For i = Sheets("Tulosta").Range("C3").Value To Sheets("Tulosta").Range("C4").Value
Sheets("Lappu").Range("C2").Value = i
Sheets("Lappu").PrintOut
Next i
Application.EnableEvents = True
End Sub
So all I want to print is "Lappu" sheet in every iteration, but for some reason also "Tulosta" sheet is printed and it is the first page that is printed.
Where is the problem?
Here is the workaround kind of solution to my problem:
Sub Painike_Napsauta()
Dim i As Long
If MsgBox("Tulosta?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
Cancel = True
Application.EnableEvents = False
Sheets("Lappu").Range("C1").Value = Sheets("Tulosta").Range("C2").Value
Sheets("Lappu").Range("C2").Value = Sheets("Tulosta").Range("C3").Value
Sheets("Lappu").Activate
Application.Dialogs(xlDialogPrint).Show
For i = Sheets("Lappu").Range("C2").Value To Sheets("Tulosta").Range("C4").Value - 1
Sheets("Lappu").Range("C2").Value = i + 1
ActiveSheet.PrintOut
Next i
Application.EnableEvents = True
End Sub
So I first select "Lappu" sheet the active one with .Activate and then printout the active sheet with ActiveSheet.PrintOut

Workbook Before Close Not Working for some but working for others

I have the following codes which is working for some people but not others.
The following does the reverse at opening the spreadsheet.
Could it be a setting it excel?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Control As Office.CommandBarControl
Application.CommandBars("ply").Enabled = True 'disables right-Click on
sheet tab
For Each Control In Application.CommandBars.FindControls(ID:=21)
'disables CUT
Control.Enabled = True
Next Control For Each Control In
Application.CommandBars.FindControls(ID:=19) 'disables COPY
Control.Enabled = True
Next Control
Application.DisplayFormulaBar = True
Application.CutCopyMode = True
Application.CellDragAndDrop = True
End Sub

Access password blocked sheet - Excel

The code below restricts access by hiding a sheet unless a password is entered. If it is entered correctly, the sheet can be viewed from the individual tabs. However, it won't let me view and then edit the sheet.
Can this be adjusted to allow the user to enter a password and then view and edit the sheet?
Private Sub Workbook_Open()
Sheets("Sheet1").Visible = xlSheetHidden
End Sub
Public ViewAccess As Boolean 'In restricted sheet's activate event
Private Sub Worksheet_Activate()
If ViewAccess = False Then
Me.Visible = xlSheetHidden
Response = Application.InputBox("Password", xTitleId, "", Type:=2)
If Response = "123" Then
Me.Visible = xlSheetVisible
Application.EnableEvents = True
ViewAccess = True
End If
End If
End Sub
Following code will help you. When a user will select a sheet with name HiddenSheet it will ask for password. If password is correct then it will allow for editing data otherwise will go to previous sheet automatically, You have to change HiddenSheet for your sheet name.
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim MySheetName As String
MySheetName = "HiddenSheet" 'The sheed which I want to hide.
If Application.ActiveSheet.Name = MySheetName Then
Application.EnableEvents = False
Application.ActiveSheet.Visible = False
response = Application.InputBox("Password", "Enter Password", "", Type:=2)
If response = "123456" Then 'Unhide Password.
Application.Sheets(MySheetName).Visible = True
Application.Sheets(MySheetName).Select
End If
End If
Application.Sheets(MySheetName).Visible = True
Application.EnableEvents = True
End Sub
Code snipped:

Excel Group of image hiding on another sheet checkbox

I'm trying to hide a bunch of images i have placed into a group via a checkbox, I can do this via the same sheet but no on the sheet the textbox is.
Sub hideimages()
If ActiveSheet.CheckBoxes("Check Box 1").Value = 1 Then
ActiveSheet.Shapes("Group 21").Visible = True
Else: ActiveSheet.Shapes("Group 21").Visible = False
End If
End Sub
But i can't seem to figure out the right syntax to get it to affect another sheet for the group I can do it for a singular image:
Sub CheckBox33_Click()
Dim obj As Shape
Set obj = Worksheets("sheet3").Shapes("picture 2")
If obj.Visible Then
obj.Visible = True
Else
obj.Visible = False
End If
How could i merge these? the ways i have tried are not happy!
Sub hidaway()
If Worksheets("sheet1").CheckBoxes("Check Box 34").Value = 1 Then
Worksheets("sheet3").group("Group 21").Visible = True
Else: Worksheets("sheet3").group("Group 21").Visible = False
End If
End Sub
Your checkbox returns True/False so you just need to feed this value to your group visible property:
Private Sub CheckBox1_Click()
ThisWorkbook.Worksheets("Sheet3").Shapes("Group 21").Visible = Me.CheckBox1.Value
End Sub

Resources