Consulting

Results 1 to 7 of 7

Thread: Solved: Unlock all worksheets

  1. #1

    Solved: Unlock all worksheets

    OK, quick question. What is the command for unlocking ALL worksheets in a workbook? I recorded a macro to unlock the active work but I need to modify the coding. Here's what I have thus far.[VBA] Sub Unlock_Workbook()
    '
    ' Unlock_Workbook Macro
    ' Macro recorded 10/7/2005 by John
    '
    ' Keyboard Shortcut: Ctrl+Shift+U
    '
    ActiveWorkbook.Unprotect Password:="unlockme"
    End Sub[/VBA]

    Rather than manually selecting each worksheet, I'd like VBA to unlock all of them.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    All worksheets are password protected?

    [VBA]
    Sub Unlock_Workbook()
    Dim sh As Worksheet
    For Each sh In ActiveWorkbook.Worksheets
    sh.Unprotect Password:="unlockme"
    Next sh
    End Sub
    [/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thank you XLD - that works fine. Now another quick follow up question. How do you tell Excel to run that code for whatever workbook is opened? For instance, I've got a folder for "Letters Sent" and a subfolder under that for all the hospitals we deal with. So using Windows Explorer, I open up each hospital's folder and double-click on the Excel file found.

    How do you get VBA to automatically execute that for whatever file is opened?

    BTW, what does the "Dim" mean?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by JohnnyBravo
    Thank you XLD - that works fine. Now another quick follow up question. How do you tell Excel to run that code for whatever workbook is opened? For instance, I've got a folder for "Letters Sent" and a subfolder under that for all the hospitals we deal with. So using Windows Explorer, I open up each hospital's folder and double-click on the Excel file found.

    How do you get VBA to automatically execute that for whatever file is opened?

    BTW, what does the "Dim" mean?
    Not tested, I quickly knocked this code up from some code snippets I have, but it should be fine - just be warned. It automatically opens all those files and processes them.

    [VBA]
    Option Explicit

    Dim oFSO

    Sub LoopFolders()
    Dim i As Integer

    Set oFSO = CreateObject("Scripting.FileSystemObject")

    selectFiles "C:\Letters Sent"

    Set oFSO = Nothing

    End Sub

    '--------------------------------------------------------------------------*-
    Sub selectFiles(sPath)
    '--------------------------------------------------------------------------*-
    Dim oFolder As Object
    Dim oFiles As Object
    Dim oFile As Object
    Dim oFldr
    Dim oWB As Workbook

    Set oFolder = oFSO.GetFolder(sPath)
    Set oFiles = oFolder.Files
    For Each oFile In oFiles
    If oFile.Type = "Microsoft Excel Worksheet" Then
    Set oWB = Workbooks.Open(Filename:=oFile.Path)
    Unlock_Workbook oWB
    oWB.Save
    oWB.Close
    End If
    Next oFile

    For Each oFldr In oFolder.Subfolder
    selectFiles oFldr.Path
    Next

    Set oWB = Nothing
    Set oFiles = Nothing
    Set oFolder = Nothing

    End Sub

    '--------------------------------------------------------------------------*-
    Sub Unlock_Workbook(wb As Workbook)
    '--------------------------------------------------------------------------*-
    Dim sh As Worksheet
    For Each sh In wb.Worksheets
    sh.Unprotect Password:="unlockme"
    Next sh
    End Sub

    [/VBA]

    Dim means Dimension a variable, that is dedclare it and type it.

    .
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi JohnnyBravo,

    If you want to have the same code available for every workbook you may have opened, the simplest way's probably to place the code in 'Personal.xls' and you can then call on it whenever it's needed.

    i.e. place xlds' original code (below) in Personal.xls ....
    Quote Originally Posted by xld
    All worksheets are password protected?

    [VBA]
    Sub Unlock_Workbook()
    Dim sh As Worksheet
    For Each sh In ActiveWorkbook.Worksheets
    sh.Unprotect Password:="unlockme"
    Next sh
    End Sub
    [/VBA]
    HTH,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    Thanks for all your guys - I really appreciate it.

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hello, I'm going to mark this as Solved.

Posting Permissions

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