+ Reply to Thread
Results 1 to 5 of 5

Protecting multiple workbooks in a directory - Automation

Hybrid View

  1. #1
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Protecting multiple workbooks in a directory - Automation

    Hi,

    These macros should lock and unlock all excel files in a directory, password is "test"

    
    Sub ProtectBooks()
        Dim wBk As Workbook
        Dim sType As String
        Dim sPath As String
        Dim sFile As String
    
        sPath = "C:\Users\dave\Desktop\New folder (2)\"
        sType = "*.xlsx"
    
        sFile = Dir(sPath & sType)
        Do While sFile <> ""
            Set wBk = Workbooks.Open(sPath & sFile)
            With wBk
                Application.DisplayAlerts = False
                wBk.SaveAs Filename:=.FullName, Password:="test"
                Application.DisplayAlerts = True
            End With
            Set wBk = Nothing
            Workbooks(sFile).Close False
            sFile = Dir
        Loop
    
    End Sub
    Sub unProtectBooks()
        Dim wBk As Workbook
        Dim sType As String
        Dim sPath As String
        Dim sFile As String
    
        sPath = "C:\Users\dave\Desktop\New folder (2)\"
        sType = "*.xlsx"
    
        sFile = Dir(sPath & sType)
        Do While sFile <> ""
            Set wBk = Workbooks.Open(sPath & sFile, , , , "test")
            With wBk
                Application.DisplayAlerts = False
                wBk.SaveAs Filename:=.FullName, Password:=""
                Application.DisplayAlerts = True
            End With
            Set wBk = Nothing
            Workbooks(sFile).Close False
            sFile = Dir
        Loop
    
    End Sub
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  2. #2
    Registered User
    Join Date
    10-26-2015
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    19

    Re: Protecting multiple workbooks in a directory - Automation

    Sweep, works like an absolute charm.

    Thank you so very much for your time and help.

    Kindest regards,

    Sam

  3. #3
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Protecting multiple workbooks in a directory - Automation

    Hi,

    Just an addenum as rightly suggested by LJMetzger, the addition of

    Application.EnableEvents = False
    to disable rogue Workbook_Open() occurrences

+ 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. VBA for multiple workbooks in a directory
    By jone kim in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-30-2014, 09:17 PM
  2. Open multiple workbooks in directory, copy a range to them for data collection
    By agutman50 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-28-2014, 01:24 PM
  3. Sort first column of multiple workbooks from a single directory
    By lmolokin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2012, 05:15 PM
  4. Replies: 0
    Last Post: 01-21-2012, 11:48 PM
  5. Copying range into multiple workbooks in a directory
    By tinkerbelle in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2007, 04:43 AM
  6. [SOLVED] Protecting Multiple Workbooks?
    By Bosco in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2005, 03:06 PM
  7. header onto multiple workbooks in same directory
    By AmyTaylor in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-13-2005, 07:17 AM

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