So I have these simple subs to unprotect than protect stuff while my macros are running, only problem is that some of my sheets are actually charts and they don't get protected when these subs are called. How can I change my code to incorporate the Charts as well? Thanks!
Sub UnprotectAll()
Dim AdminPassword As String
AdminPassword = "password"
ActiveWorkbook.Unprotect ("Evaluate(AdminPassword)")
For Each sh In ActiveWorkbook.Worksheets
sh.Unprotect Password:=AdminPassword
Next sh
End Sub
Sub ProtectAll()
Dim AdminPassword As String
AdminPassword = "password"
ActiveWorkbook.Protect ("Evaluate(AdminPassword)")
For Each sh In ActiveWorkbook.Worksheets
sh.Protect Password:=AdminPassword
Next sh
End Sub
Replace ActiveWorkbook.Worksheets by ActiveWorkbook.Sheets
Related
I have two simple pieces of code in a Userform and what I want to do, I believe is quite straight forward however I'm stuck!
Private Sub UserForm_Initialize()
Dim wkb As Workbook
With Me.CB_Excel_File
For Each wkb In Application.Workbooks
.AddItem wkb.Name
Next wkb
End With
End Sub
Private Sub CB_Excel_File_Change()
Dim wks As Worksheet
With Me.CB_Worksheet
For Each wks In ***Me.CB_Excel_File.Value.Worksheets***
.AddItem wks.Name
Next wks
End With
End Sub
Its the piece with *** that I'm stuck on as I want to list all of the worksheets in the workbook that is selected by the user from the Userform_Initialize code.
Thanks in advance!
You need to pass the selected workbook name into the Workbooks collection as a variable, as so:
Private Sub CB_Excel_File_Change()
Dim wks As Worksheet
With Me.CB_Worksheet
For Each wks In Workbooks(CB_Excel_File.Value).Worksheets
.AddItem wks.Name
Next wks
End With
End Sub
I have a workbook that has a number of worksheets each one with a colaborator name on it (Windows login username).
I've tried via VBA to loop through all Worksheets to match the actual Windows Logged On username with the matching Worksheet, and after the match is done, only that Worksheet is kept visible (with all the others being hiden).
I've managed to do this partially but i can only do it untill it finds the matching worksheet. For example, if the matching username is the third worksheet (in a total of ten for example) the code stops there. I want it to run through all worksheets and only then hide the non matching Worksheets.
First i have the following module:
Sub WorksheetFilter()
Dim Username As String
Dim Worksheetname As String
Worksheetname = ActiveWorkbook.ActiveSheet.Name
Username = Environ("Username")
If Worksheetname <> Username Then
ActiveSheet.Visible = False
End If
End Sub
Then, i call the previous module on the Workbook_Open() event:
Option Explicit
Dim WS As Worksheet
Private Sub Workbook_Open()
For Each WS In ActiveWorkbook.Worksheets
WorksheetFilter
Next
End Sub
Any hints on how this can be achieved?
Thanks,
VĂtor
Use the code below, and put it in the Workbook module under the Workbook_Open event.
Just loop through all sheets and compare each one with the username.
Option Explicit
Public Sht As Worksheet
Private Sub Workbook_Open()
For Each Sht In ThisWorkbook.Sheets
If Sht.Name = Environ("Username") Then
Sht.Visible = xlSheetVisible
Else
Sht.Visible = xlSheetHidden
' option 2: use very hidden, only able to unhide through code (unable to unhide using right-click)
'Sht.Visible = xlSheetVeryHidden
End If
Next Sht
End Sub
Please see below: chopped your code around a bit. You do not need to defien the worksheets name. This is for the module level, you can call it in the workbook open event as per usual
Option Explicit
Dim ws As Worksheet
Dim Username As String
Sub WorksheetFilter()
Username = Environ("Username")
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> Username Then
ws.Visible = False
Else
ws.Visible = True
End If
Next ws
End Sub
Please let me know how this works for you! :)
I have this code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
Sheets("MACROS").Visible = True
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "MACROS" Then
ws.Visible = xlVeryHidden
End If
Next ws
Application.CommandBars("Ply").Enabled = True
End Sub
The code displays the MACROS sheet when macros are disabled. The thing is that when macros are enabled, and some work had been done on the workbook, and the book is closed by clicking the "X" (Close Button), it prompts to save but displays the MACROS sheet.
I am looking to have the program remain on active sheet while displaying save prompt.
Would someone be so kind to please help me with modifying the above code? All and any help will be greatly appreciated!
Remove the line Sheets("MACROS").Visible = True.
The code should be:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "MACROS" Then
ws.Visible = xlVeryHidden
End If
Next ws
Application.CommandBars("Ply").Enabled = True
End Sub
I have this macro for a button to refresh all the pivot-tables in a worksheet:
Sub Button3_Click()
ThisWorkbook.RefreshAll
End Sub
And I wanted to add the functionality to refresh all the pivot tables even if they are on a protected sheet. I protected the pivot-table sheet with the password MyPwd and used the below code, but it won't work:
Sub Button3_Click()
Unprotect Password:="MyPwd"
ThisWorkbook.RefreshAll
Protect Password:="MyPwd", _
DrawingObjects:=True, Contents:=True, _
Scenarios:=True, AllowUsingPivotTables:=True
End With
End Sub
Visual Basic is all new to me. What am I doing wrong?
The Unprotect you want is a worksheet method. You should qualify it.
Sub ProtRef()
Dim TargetSht As Worksheet
Set TargetSht = ThisWorkbook.Sheets("Sheet1") 'Modify as needed.
With TargetSht
.Unprotect MyPwd
ThisWorkbook.RefreshAll
.Protect MyPwd
End With
End Sub
Note TargetSht and the With-End With. Let us know if this helps.
EDIT:
Sub ProtRef()
Dim WB As Workbook, WS As Worksheet
For Each WS In WB.Worksheets
If WS.Name <> "Sheet1" Then
WS.Unprotect MyPwd
End If
End With
ThisWorkbook.RefreshAll
For Each WS In WB.Worksheets
If WS.Name <> "Sheet1" Then
WS.Protect MyPwd
End If
End With
End Sub
Paste in a regular module, like below:
Let us know if this helps.
Thank you #BK201 for having a crack at it, you definitely pointed me in the right direction. I used this code in the button and it seemed to do the trick:
Sub Button1_Click()
Dim Sht As Worksheet
For Each Sht In ThisWorkbook.Worksheets
Sht.Unprotect Password:="MyPwd"
ThisWorkbook.RefreshAll
Next
For Each Sht In ThisWorkbook.Worksheets
Sht.Protect Password:="MyPwd"
Next
End Sub
I was wondering if it is possible to check for a particular sheets for its availability. If it is around, it will continue on with the rest of the code. If not around then it will add in the sheet.
I have thought of it but it is giving me error. Do share some info if u know something! thanks!
sub macro1()
If sheets("Test") = False Then
Sheets.Add.Name = "Test"
End If
'Run my code
End Sub
Like this?
Sub Sample()
Dim ws As Worksheet
On Error Resume Next
Set ws = Sheets("Test")
On Error GoTo 0
If ws Is Nothing Then
Set ws = Sheets.Add
ws.Name = "Test"
End If
'~~> Run your code
End Sub
Another approach ... create a function that
- accepts a workbook object and the name of the sheet you're after and
- returns tru if the sheet is found in the workbook
Function SheetExists(oWorkbook As Workbook, sSheetname As String)
Dim oWs As Worksheet
For Each oWs In oWorkbook.Worksheets
If oWs.Name = sSheetname Then
SheetExists = True
Exit Function
End If
Next
End Function
Sub TestSheetExists()
If SheetExists(ActiveWorkbook, "Bob") Then
MsgBox "Found it"
Else
MsgBox "No joy"
End If
End Sub