+ Reply to Thread
Results 1 to 12 of 12

VBA and password protect sheet.

Hybrid View

  1. #1
    Registered User
    Join Date
    02-27-2012
    Location
    Helsingborg
    MS-Off Ver
    Excel 2010
    Posts
    8

    Wink VBA and password protect sheet.

    Hi!
    I have a question regarding the macro below. As you can see the macro loops trough a droplist and copy the sheet to a new one and saves it. My question is if it's possible to have the new sheets password protected? In my case I would like to have diffrent password for each file...

    I'm getting Error 424 - Object Required, any idéa how to fix this one?

    Sub Sammanfattning_Save()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Sheets("Sammanställning").Select
     
     Dim rng As Range, cell As Range
     Set rng = Evaluate(ActiveSheet.Range("G1").Validation.Formula1)
    
    t = rng(1, 1).Row
    
     For Each cell In rng
     
     ActiveSheet.Range("G1").Value = cell.Value
     Sheets("Sammanställning").Copy
        
        Dim FName           As String
        Dim FPath           As String
            
        FPath = "C:\Users\Hemma\Documents\My Dropbox\Kristofer\Jobb\Sammanställningar"
        FName = Sheets("Sammanställning").Range("G1").Text
        ActiveSheet.SaveAs Filename:=FPath & "\" & FName, Password:=Sheets("Password").Range("A1").Offset(cl.Row - t, 0).Value, ReadOnlyRecommended:=False, CreateBackup:=False
        
    ActiveWindow.Close
    Next
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
    End Sub
    I really hope you can help me with this one =)

    Regards,
    Christoffer
    Last edited by esbjornsson; 02-29-2012 at 03:22 AM. Reason: Solved

  2. #2
    Registered User
    Join Date
    11-23-2011
    Location
    VietNam
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: VBA and password protect sheet.

    Password:=Sheets("Password").Range("A1").Offset(cl.Row - t, 0).Value
    cl????????
    Change

    Password:=Sheets("Password").Range("A1").Offset(cell.Row - t, 0).Value

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

    Re: VBA and password protect sheet.

    Try this,

    activesheet.Protect Password:="123"
        ActiveSheet.SaveAs Filename:=FPath & "\" & FName, .........

  4. #4
    Registered User
    Join Date
    02-27-2012
    Location
    Helsingborg
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: VBA and password protect sheet.

    First, thanks for trying to help me =)
    Hmm, changed cl to cell...

    The error 424 has now change to "Subscript out of range (Error 9)"
    Is it because of the Sheet.copy command?

    @davesexcel: I want to have diffrent passwords assigned to the files... Security isn't important in this case. I have written all passwords in a a sheet called "Password".
    Last edited by esbjornsson; 02-27-2012 at 10:57 PM.

  5. #5
    Registered User
    Join Date
    02-27-2012
    Location
    Helsingborg
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: VBA and password protect sheet.

    Sorry... The code you suggested will assign the same password to each file?

  6. #6
    Registered User
    Join Date
    11-23-2011
    Location
    VietNam
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: VBA and password protect sheet.

    Can You post file? I think cells in yoursfile having special characters. With Protect in excel, Never a good way

  7. #7
    Registered User
    Join Date
    02-27-2012
    Location
    Helsingborg
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: VBA and password protect sheet.

    http://dl.dropbox.com/u/4400615/Samm...%20elever.xlsm
    Hi again!
    You can download the file from the url above.
    Thanks!

  8. #8
    Registered User
    Join Date
    02-27-2012
    Location
    Helsingborg
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: VBA and password protect sheet.

    Thank you so much!

    Have a nice day =)

  9. #9
    Registered User
    Join Date
    02-28-2012
    Location
    Adelaide
    MS-Off Ver
    excel 2010
    Posts
    1

    Re: VBA and password protect sheet.

    Hi i have done something similar with our timesheets here at work.
    I give the user the ability to approve sheets then secure the worksheet after with a password.
    Why are you looking to use different passwords. This can be done but may be a problem in future trying to unlock.

  10. #10
    Registered User
    Join Date
    02-27-2012
    Location
    Helsingborg
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: VBA and password protect sheet.

    Quote Originally Posted by Davabb View Post
    Hi i have done something similar with our timesheets here at work.
    I give the user the ability to approve sheets then secure the worksheet after with a password.
    Why are you looking to use different passwords. This can be done but may be a problem in future trying to unlock.
    Hi.
    I'm a teacher and want to hand out results and attendance each week to the pupils parents. Thats why I don't want the same password for every file.
    I upload the files to a dropbox-folder...

    hi esbjornsson

    If your problem is solve then Mark your thread as Solved
    Done! Thanks for the reminder =)

  11. #11
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Smile Re: VBA and password protect sheet.

    Hi Friend,

    i made some changes in your code
    i fix the name in validation list because i got "Application error Problem" you may manage it

    Sub Sammanfattning_Save()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Sheets("Sammanställning").Select
    
    Dim sPassword As String
    Dim i As Integer
    Dim shtPassword As Worksheet
    Set shtPassword = Sheets("Password")
    Dim shtSamm As Worksheet
    Set shtSamm = Sheets("Sammanställning")
    
     Dim rng As Range, cell As Range
     Set rng = Evaluate(ActiveSheet.Range("G1").Validation.Formula1)
    ' Set rng = Evaluate(shtSamm.Range("G1").Validation.Formula1)
    t = rng(1, 1).Row
    i = 1
    
    With shtPassword
    
        
             For Each cell In rng
             
             ActiveSheet.Range("G1").Value = cell.Value
             Sheets("Sammanställning").Copy
                
                Dim FName           As String
                Dim FPath           As String
                
                sPassword = .Range("A" & i).Value
                
                FPath = "C:\Documents and Settings\pdsys3\Desktop\New Folder"
                FName = Sheets("Sammanställning").Range("G1").Text
                ActiveSheet.SaveAs Filename:=FPath & "\" & FName, Password:=sPassword, ReadOnlyRecommended:=False, CreateBackup:=False
               
               i = i + 1
            
            ActiveWindow.Close
            Next
        
     End With


    See attached file
    Attached Files Attached Files

  12. #12
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: VBA and password protect sheet.

    hi esbjornsson

    If your problem is solve then Mark your thread as Solved

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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