+ Reply to Thread
Results 1 to 7 of 7

VBA password protect workbook after set date

Hybrid View

  1. #1
    Registered User
    Join Date
    12-16-2020
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    28

    VBA password protect workbook after set date

    Hi,

    Upon opening my workbook I am trying to make it become password protected, however this will only be applicable after a specific date (10/09/2021). i currently have the following code but it is displaying syntax errors:

    PHP Code: 
    Private Sub Workbook_Open()
    Dim d1 As Date
    Dim d2 
    As Date
    Dim password 
    As String
    d1 
    10 2021
    d2 
    Date
    If d2 d1 Then
    password 
    InputBox (“enter password”)
    Else
    MsgBox (“Opening file”)
    End If
    If 
    password “abC123_ Then
    MsgBox 
    (“Welcome!)
    Else
    MsgBox (“Incorrect password!)
    password InputBox(“enter password again”)
    End If
    End Sub 
    Any help would be much appreciated.

    Thanks

  2. #2
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: VBA password protect workbook after set date

    With these changes it should work better.
    Option Explicit
    Private Sub Workbook_Open()
        Dim d1     As Date
        Dim d2     As Date
        Dim password As String
        d1 = CDate("10/02/2021")
        d2 = Date
        If d2 > d1 Then
            password = InputBox("enter password")
        Else
            MsgBox ("Opening file")
            Exit Sub
        End If
    pwd:
        If password = "abC123_" Then
            MsgBox ("Welcome!")
            Exit Sub
        Else
            MsgBox ("Incorrect password!")
            password = InputBox("enter password again")
            If password = "" Then
            MsgBox ("No password ... no party ...")
            ThisWorkbook.Close False
            End If
            GoTo pwd
        End If
    End Sub

  3. #3
    Registered User
    Join Date
    12-16-2020
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    28

    Re: VBA password protect workbook after set date

    Great thank you so much, that working perfectly from what i asked. As a slight adjustment to my initial request, is there a way to just open the workbook with no message box when its before the expiry date. So to change this part?
    PHP Code: 
    Else
            
    MsgBox ("Opening file")
            Exit 
    Sub
        End 
    If 

  4. #4
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: VBA password protect workbook after set date

    Hi,

    try this,

    Private Sub Workbook_Open()
       Dim d1 As Date
       Dim d2 As Date
       Dim password As String
       
       d1 = CDate("10/09/2021")
       d2 = Date
       
       If d2 > d1 Then
          While password <> "abC123_"
             password = InputBox("enter password", "Locked", "XXXX")
             If password = "abC123_" Then
                MsgBox "Welcome!", vbOKOnly
               Else
                MsgBox "Incorrect password!", vbCritical
             End If
          Wend
       End If
       
    End Sub
    Last edited by Joske920; 02-12-2021 at 01:19 PM.
    Please be as complete as possible in your asking so it may save use all the time to rework the solution because you didn't give all the requirements. If you have a layout in mind please work it out first so we can adapt our solution to it. Thanks.
    If you have been helped, maybe you could click the *

  5. #5
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: VBA password protect workbook after set date

    Oh, yes, just get rid of the Msgbox; sincerely I thought that most of the MsgBoxs were there only for debugging.

  6. #6
    Registered User
    Join Date
    12-16-2020
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    28

    Re: VBA password protect workbook after set date

    Thanks, sorry fairly new to VBA and i worked that out just after i pressed send. Thanks for your help

  7. #7
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: VBA password protect workbook after set date

    Glad we have been of some help .

+ 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. Password Protect Workbook
    By adam2308 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-15-2016, 05:37 AM
  2. Replies: 2
    Last Post: 11-17-2014, 04:28 PM
  3. Replies: 3
    Last Post: 07-01-2014, 02:48 PM
  4. Replies: 1
    Last Post: 04-12-2012, 03:42 AM
  5. Excel 2007 : Password protect workbook
    By shuddle in forum Excel General
    Replies: 2
    Last Post: 11-23-2011, 03:00 PM
  6. password protect workbook
    By rob in forum Excel General
    Replies: 1
    Last Post: 08-16-2006, 07:00 AM
  7. Replies: 2
    Last Post: 07-16-2005, 11:05 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