Refresh protected pivot-table with a button? - vba

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

Related

VBA Code to Allow Group / Ungroup in a Protected Workbook

I am using the code below to protect and save the workbook automatically upon closing. However, this code isn't allowing me to use the group / ungroup feature when I re-open the workbook. Can I edit this code to allow the group / ungroup?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Protect Password:="your password", UserInterfaceOnly:=True, AllowFormattingColumns:=True
ws.EnableOutlining = True
Next ws
ThisWorkbook.Protect "your password", True
ThisWorkbook.Save
End Sub
I tried using the code below to allow the Group/Ungroup on opening, but it doesn't work the way I intended. I had to insert a button into the worksheet. The code does work with the use of a button. I was hoping for one automatic code to solve the group/ungroup problem which doesn't use a button.
Sub Workbook_Open()
For Each ws In Sheets
With ws
.Unprotect Password:="your password"
.Protect Password:="your password", UserInterfaceOnly:=True
.EnableOutlining = True
End With
Next ws
End Sub

Making VBA apply to renamed tabs & all tabs in a workbook

I don't know very much at all about VBA, but I found the below code on a website and am using it in a workbook.
Private Sub Workbook_Open()
With Worksheets("WFD")
.EnableOutlining = True
.Protect Password:="XXXX", _
Contents:=True, UserInterfaceOnly:=True
End With
End Sub
How should I amend this so that if the Sheet name is changed from "WFD" to something else, the code still works? Also I would like it to apply to all sheets in the workbook.
Thanks very much
If you want this code for each worksheet use code below:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
With ws
.EnableOutlining = True
.Protect Password:="XXXX", _
Contents:=True, UserInterfaceOnly:=True
End With
Next
End Sub
You should use the Sheet Object Codename.
This is found in the VB Editor beside the sheet objects in the VB project.
By default they are Sheet1, Sheet2 etc. You can easily change them by clicking and typing a new name etc.
You could of course leave them as default codeName if you like...
This is NOT the same as the worksheet name, which is changed by users on the Sheet tabs in Excel interface.
Private Sub Workbook_Open()
With WFD 'where WFD is the CODENAME of the Sheet Object!
.EnableOutlining = True
.Protect Password:="XXXX", _
Contents:=True, UserInterfaceOnly:=True
End With
End Sub
You could write Worksheets(1) or Worksheets(5), depending on the number of the Worksheet. However, if you start adding and deleting Worksheets, it whould not work (e.g., it would be working on some other worksheet). To see the associated number to the worksheet, run this and check the immediate window:
Option Explicit
Public Sub TestMe()
Dim ws As Worksheet
Dim cnt As Long
For cnt = 1 To ThisWorkbook.Worksheets.Count
Debug.Print cnt; "-> "; Worksheets(cnt).name
Next cnt
End Sub
However, if you have only one Worksheet, Worksheets(1) would always work.

Excel VBA will not loop through worksheets (Exclude Worksheet)

The macro appears to loops through all of the worksheets fine now. However, is there a way that I can make it so the macro does not get applied to a specific worksheet in my workbook, but does get applied to all other worksheets?
Sub FormatSheet()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
Columns("A:J").Select
Selection.AutoFilter
Columns("A:J").EntireColumn.AutoFit
With Selection
.HorizontalAlignment = xlCenter
End With
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
Next ws
End Sub
I created a dataset to test this out and found that the code worked just fine. So it must be something related to your specific data/worksheets. I would see if 2 sheets works, or try making a smaller sample on another workbook and see if it works. Sorry I'm not more helpful.
Consider this approach.
Option Explicit
Sub test()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then
With ws.UsedRange
.Resize(.Rows.Count-1).Offset(1, 0).ClearContents
End With
End If
Next ws
End Sub

Protect/Unprotect chart sheets

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

Apply a protected sheet macro to all sheets in workbook

I have a macro that opens automatically and applies a special sheet protection which allows for grouping and some editing for a specific sheet, Program Data. The macro I have is this:
Private Sub Workbook_Open()
With Worksheets("Program Data")
.EnableOutlining = True
.Protect UserInterfaceOnly:=True, AllowFiltering:=True, AllowFormattingColumns:=True,
AllowInsertingRows:=True
End With
End Sub
Right now this runs as soon as you open the file, but it only applies to Program Data. I don't know how to change the with statement to accommodate the entire workbook without naming the sheets one-by-one. But there are very many sheets, so that's not a good option. How do I do apply this for all sheets in the file?
You can loop through the worksheet objects like this.
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
.EnableOutlining = True
.Protect UserInterfaceOnly:=True, AllowFiltering:=True, _
AllowFormattingColumns:=True, AllowInsertingRows:=True
End With
Next
End Sub
You want a For loop that targets all the sheets. Basically...
Private Sub Workbook_Open()
Dim WS As Worksheet
For Each WS in ThisWorkbook.Worksheets
With WS
.EnableOutlining = True
.Protect UserInterfaceOnly:=True, AllowFiltering:=True, AllowFormattingColumns:=True,
AllowInsertingRows:=True
End With
Next WS
End Sub
Let us know if this helps.
try this
For i = 1 To Worksheets.Count - 1
With Worksheets(i)
.EnableOutlining = True
.Protect UserInterfaceOnly:=True, AllowFiltering:=True, AllowFormattingColumns:=True, AllowInsertingRows:=True
End With
Next