• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Use name manager name as sheet password|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Use name manager name as sheet password|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosUse name manager name as sheet pass…
sp_PrintTopic sp_TopicIcon
Use name manager name as sheet password
Avatar
Shawn Wallack
Member
Members
Level 0
Forum Posts: 77
Member Since:
December 7, 2021
sp_UserOfflineSmall Offline
1
May 25, 2022 - 1:19 pm
sp_Permalink sp_Print sp_EditHistory

I want to protect the content and objects on the sheets in my workbook, but I need my VBA to be able to manipulate them. I thought maybe I could add a named range in the Workbook_Open() procedure and then refer to the value in that range when I need to know what the password is. The problem is that the procedures run but the password is not set when re-protecting the sheets.

Something like this in the "ThisWorkbook" object:    

Private Sub Workbook_Open()

  Dim ws As Worksheet
  ' Create a named range called "pswd"
  ' Assign the value "TrainingHub" to the range
  ThisWorkbook.Names.Add "pswd", "TrainingHub"

  msgbox "The password is: " & [pswd]

  ' Now protect every sheet with the password
  For Each ws In ThisWorkbook.Worksheets
    Debug.Print "Protecting sheet " & ws.Name & " with password " & [pswd]
    ws.Protect Password:=[pswd]

    ' Not sure if I need this or not. And if so, not sure how to use it!
    ' ws.Protect UserInterFaceOnly:=True
  Next ws

End Sub

 

Sub DoSomething

  ' Unprotect sheet before action
  If ProtectSheet (False) = True then
    debug.print "Unprotect sheet SUCCESS"
  Else
   debug.print "Unprotect sheet FAILED"
  Endif

  ' Perform some action here, such as adding new slicers (this is why I need to unprotect the sheet)

 ' Protect the sheet before exiting
  If ProtectSheet (True) = True then
    debug.print "Protect sheet SUCCESS"
  Else
    debug.print "Protect sheet FAILED"
  Endif

End Sub

 

Function ProtectSheet (byval bSwitch as boolean) as boolean

  ProtectSheet = False

  If bSwitch = TRUE then ' Protect

    ActiveSheet.Protect Password:=[pswd], _
      DrawingObjects:=True, _
      Contents:=True, _
      Scenarios:=True
    ProtectSheet = True

  Else

    ActiveSheet.unprotect password:=[pswd]
    ProtectSheet = True

  Endif

End Function

sp_AnswersTopicSeeAnswer See Answer
Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 688
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
2
May 25, 2022 - 5:12 pm
sp_Permalink sp_Print sp_EditHistory

That code works fine here.

Just as a side note, there is no need to compare a boolean to a boolean to get the same boolean, so you can just use If bSwitch Then rather than

If bSwitch = TRUE then
Avatar
Shawn Wallack
Member
Members
Level 0
Forum Posts: 77
Member Since:
December 7, 2021
sp_UserOfflineSmall Offline
3
May 26, 2022 - 8:39 am
sp_Permalink sp_Print

I got the code to work except for one issue...

Sub ProtectThisSheet()

  If Not AddProtection(ActiveSheet.Index) Then
    MsgBox "Failed to protect sheet"
  Else
    MsgBox "Protected the sheet"
  End If

End Sub

Function AddProtection(ByVal iSheetIndex As Integer) As Boolean

  AddProtection = False

  MsgBox "START -- The sheet index passed is " & iSheetIndex & vbNewLine & "Protected = " & Sheets(iSheetIndex).ProtectContents

  ' Hard code password for the training hub forum
  Dim strPswd As String
  strPswd = "pass123"  

  On Error Resume Next

  ' This simple line of code works. The sheet gets protected with the password. The msgbox at the end proves it.
  Sheets(iSheetIndex).Protect Password:=strPswd

  ' This more complex code FAILS. The sheet does not get protected with any password. The msgbox at the end proves it.
  ' I am wondering if some property I'm setting is somehow negating the password property
  ' I want the sheet protected with a password and I need the pivot tables to work, but I want to apply these other restrictions

  Sheets(iSheetIndex).Protect Password:=strPswd, _
    AllowUsingPivotTables:=True, _
    DrawingObjects:=False, _
    Contents:=False, _
    Scenarios:=False, _
    AllowFormattingCells:=False, _
    AllowFormattingColumns:=False, _
    AllowFormattingRows:=False, _
    AllowInsertingColumns:=False, _
    AllowInsertingRows:=False, _
    AllowInsertingHyperlinks:=False, _
    AllowDeletingColumns:=False, _
    AllowDeletingRows:=False, _
    AllowSorting:=False, _
    AllowFiltering:=False

  ' Check whether we were successful
  If Err.Number = 0 Then
    AddProtection = True
    Debug.Print "Protected sheet: " & Sheets(iSheetIndex).Name

  Else
    Debug.Print "Failed to protect sheet: " & Sheets(iSheetIndex).Name
    MsgBox "Failed to protect sheet: " & Sheets(iSheetIndex).Name & _
      vbNewLine & "Protection Status = " & UCase(Sheets(iSheetIndex).ProtectContents)

  End If

  MsgBox "END -- The sheet index passed is " & iSheetIndex & vbNewLine & "Protected = " & Sheets(iSheetIndex).ProtectContents

End Function

 

Thanks!

Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 688
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
4
May 26, 2022 - 6:31 pm
sp_Permalink sp_Print

It works for me but your message box is misleading since it relies on the ProtectContents property, which you set to False in your protection.

sp_AnswersTopicAnswer
Answers Post
Avatar
Shawn Wallack
Member
Members
Level 0
Forum Posts: 77
Member Since:
December 7, 2021
sp_UserOfflineSmall Offline
5
May 27, 2022 - 10:08 pm
sp_Permalink sp_Print

Thank you

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Saliha Mohamed
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 880
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 237
Jessica Stewart: 219
A.Maurizio: 213
Aye Mu: 201
jaryszek: 183
Newest Members:
Ashley Hughes
Herbie Key
Trevor Pindling
Stevan Kanjo
Erin Sheldon
Nikita Bhatia
Sheilah Taylor
Clare Webber
David Jenssen
Dominic Brosnahan
Forum Stats:
Groups: 3
Forums: 24
Topics: 6526
Posts: 28576

 

Member Stats:
Guest Posters: 49
Members: 32810
Moderators: 2
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.