+ Reply to Thread
Results 1 to 6 of 6

Add more than one worksheet in the following vba code

  1. #1
    Registered User
    Join Date
    09-28-2017
    Location
    canada
    MS-Off Ver
    15,34
    Posts
    5

    Add more than one worksheet in the following vba code

    Hi everyone


    I really need help on this please....

    I use this vba code on a worksheets in my workbook to lock information after

    saving to prevent any modification from employees after they use the worksheet.

    It's working great but i need to know how to include more sheets now:"sheet1" + 3 other worksheets ... and maybe more in the futur.

    I don't know how to include those sheets in the following vba code.


    Thanks for your help.
    I'm new on this forum so i hope i did everything correctly and sorry for my english i'm french.
    Steve.


    Private Sub Workbook_Open()
    Dim wsh As Worksheet
    Dim rng As Range
    Set wsh = Me.Worksheets("Sheet1")
    On Error Resume Next
    wsh.Unprotect Password:="Secret"
    wsh.Cells.Locked = False
    Set rng = wsh.UsedRange.SpecialCells(xlCellTypeConstants)
    If Not rng Is Nothing Then
    rng.Locked = True
    End If
    Set rng = wsh.UsedRange.SpecialCells(xlCellTypeFormulas)
    If Not rng Is Nothing Then
    rng.Locked = True
    End If
    wsh.Protect Password:="Secret"
    End Sub

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Add more than one worksheet in the following vba code

    You can create a list of sheets in the code , then loop through them.
    For example.
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    09-28-2017
    Location
    canada
    MS-Off Ver
    15,34
    Posts
    5

    Re: Add more than one worksheet in the following vba code

    thanks for the fast answer !

    great idea! but can you tell me how to add this code to the previous one i showed to you please.. final result ? I'm just starting on vba.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Add more than one worksheet in the following vba code

    You can edit the code to suite...

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-28-2017
    Location
    canada
    MS-Off Ver
    15,34
    Posts
    5

    Re: Add more than one worksheet in the following vba code

    Sorry if i dont get it ... You mind like this ?? :

    Private Sub Workbook_Open()
    Dim wsh As Worksheet
    Dim rng As Range
    Set wsh = Me.Worksheets("Sheet1")
    On Error Resume Next
    wsh.Unprotect Password:="Secret"
    wsh.Cells.Locked = False
    Set rng = wsh.UsedRange.SpecialCells(xlCellTypeConstants)
    If Not rng Is Nothing Then
    rng.Locked = True
    End If
    Set rng = wsh.UsedRange.SpecialCells(xlCellTypeFormulas)
    If Not rng Is Nothing Then
    rng.Locked = True
    End If
    wsh.Protect Password:="Secret"
    End Sub

    Sub LckCells()
    Dim sts, sh

    sts = Array("sheet2", "sheet3", "sheet4") 'sheet names

    For Each sh In sts
    With Sheets(sh)
    On Error Resume Next
    .Unprotect Password:="Secret"
    .Cells.SpecialCells(xlCellTypeConstants, 23).Locked = True
    .Cells.SpecialCells(xlCellTypeFormulas, 23).Locked = True
    .Protect Password:="Secret"
    End With
    Next sh

    End Sub

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Add more than one worksheet in the following vba code

    If that is the sheet names then yes, you can add .cells.lock=false if you want to unlock the cells before you lock them again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copying an Existing Macro From Worksheet To Another Worksheet View Code
    By Quivolt in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-26-2017, 04:02 AM
  2. Replies: 1
    Last Post: 07-23-2016, 11:50 PM
  3. Replies: 13
    Last Post: 10-05-2015, 08:59 AM
  4. [SOLVED] VBA code to save current worksheet as temporary PDF file and then add to my existing code
    By brianfromla in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-20-2014, 08:35 AM
  5. VBA -- pouse code to enter data in a worksheet, and then resume code
    By jcm4ccc in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-26-2013, 12:59 PM
  6. Worksheet Code Launches Diifferent Worksheet Code - PivotTableUpdate
    By sample57 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-25-2013, 04:33 PM
  7. Replies: 2
    Last Post: 03-17-2011, 08:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1