Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:14:09+00:00 2026-06-03T19:14:09+00:00

This is a follow up from this question, Lock Cells after Data Entry .

  • 0

This is a follow up from this question, Lock Cells after Data Entry. I have progressed from asking that question but encountered more problems so felt I should ask a new question. The workbook is edited by multiple users. To prevent tampering with previous data the cells are locked once data has been entered and the file saved.

I have a couple of small bugs in the code:

  1. If the user chooses to SaveAs then tries to save over an existing file the usual ‘ Do you want to replace this file?’ dialog appears. If the user selects no there is a run time error. I have highlighted where the error is in the code below but I am unsure how to fix it.

  2. If the user has entered data then tries to exit and save the file using the save dialog box that appears on close the file is saved but the data is not locked. I have been trying to call my main code to lock the cells upon an exit save but I keep encountering argument not optional errors.

Here is the full code:

Option Explicit
Const WelcomePage = "Macros"
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Written by Alistair Weir (alistair.weir@communitypharmacyscotland.org.uk, http://alistairweir.blogspot.co.uk/)

Dim ws As Worksheet
Dim wsActive As Worksheet
Dim vFilename As Variant
Dim bSaved As Boolean

'Turn off screen updating
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

'Record active worksheet
Set wsActive = ActiveSheet

'Prompt for Save As
If SaveAsUI = True Then
    MsgBox "Are you sure you want to save? Data entered cannot be edited once the file has been saved. Press cancel on the next screen to edit your data or continue if you are sure it is correct.", vbCritical, "Are you sure?"

    vFilename = Application.GetSaveAsFilename("", fileFilter:="Excel Files (*.xls), *.xls")
    If CStr(vFilename) = "False" Then
        bSaved = False
    Else
        'Save the workbook using the supplied filename
        Call HideAllSheets
        '--> The vFilename Variant in the next line is the problem **
        '--> when trying to overwrite an existing file  **
        ThisWorkbook.SaveAs vFilename
        Application.RecentFiles.Add vFilename
        Call ShowAllSheets
        bSaved = True
    End If
Else
    'Save the workbook, prompt if normal save selected not save As
    Call HideAllSheets
    If MsgBox("Are you sure you want to save? Data entered cannot be edited after saving", vbYesNo, "Save?") = vbYes Then
        ThisWorkbook.Save
        Call ShowAllSheets
        bSaved = True
        Else
        Cancel = True
    End If
    Call ShowAllSheets
End If

'Restore file to where user was
wsActive.Activate
'Restore screen updates
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With

'Set application states appropriately
If bSaved Then
    ThisWorkbook.Saved = True
    Cancel = True
Else
    Cancel = True
End If

'Lock Cells before save if data has been entered
    Dim rpcell As Range
With ActiveSheet
    If bSaved = True Then
    .Unprotect Password:="oVc0obr02WpXeZGy"
    .Cells.Locked = False
    For Each rpcell In ActiveSheet.UsedRange
        If rpcell.Value = "" Then
            rpcell.Locked = False
        Else
            rpcell.Locked = True
        End If
    Next rpcell
    .Protect Password:="oVc0obr02WpXeZGy"
    Else
    MsgBox "The LogBook was not saved. You are free to edit the RP Log again", vbOKOnly, "LogBook Not Saved"
    End If
End With

End Sub

Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    Call ShowAllSheets
    Application.ScreenUpdating = True
    ThisWorkbook.Saved = True
End Sub

'Called to hide all the sheets but enable macros page
Private Sub HideAllSheets()
    Dim ws As Worksheet
    Worksheets(WelcomePage).Visible = xlSheetVisible
    For Each ws In ThisWorkbook.Worksheets
        If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVeryHidden
    Next ws
    Worksheets(WelcomePage).Activate
End Sub

'Called to show the data sheets when macros are enabled
Private Sub ShowAllSheets()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVisible
    Next ws
    Worksheets(WelcomePage).Visible = xlSheetVeryHidden
End Sub

Thanks 🙂

Edit

For now I am solving problem 2 by bypassing excel’s default ‘do you want to save?’ by doing this:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    If MsgBox("Are you sure you want to quit? Any unsaved changes will be lost.", vbYesNo, "Really quit?") = vbNo Then
    Cancel = True
    Else
    ThisWorkbook.Saved = True
    Application.Quit
    End If

End Sub

I am open to suggestions of a better way and still haven’t solved the first problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:14:18+00:00Added an answer on June 3, 2026 at 7:14 pm

    One possibility is to write your own confirmations in a save function, like so:

    Private Function SaveSheet(Optional fileName) As Boolean
    
    HideAllSheets
    
    If fileName = "" Then
        ThisWorkbook.Save
        SaveSheet = True
    Else
        Application.DisplayAlerts = False
    
        If Dir(fileName) <> "" Then
            If MsgBox("Worksheet exists. Overwrite?", vbYesNo, "Exists") = vbNo Then Exit Function
        End If
    
        ThisWorkbook.saveAs fileName
        SaveSheet = True
    
        Application.DisplayAlerts = True
    End If
    
    ShowAllSheets
    
    End Function
    

    And change your original code to something like:

    If SaveAsUI Then
        If MsgBox( _
            "Are you sure you want to save? Data entered cannot be edited once the file has been saved. " & _
            "Press cancel on the next screen to edit your data or continue if you are sure it is correct.", _
            vbYesNo, "Are you sure?" _
        ) = vbYes Then
            vFilename = Application.GetSaveAsFilename("", fileFilter:="Excel Files (*.xls), *.xls")
    
            If vFilename <> "" Then
                If SaveSheet(vFilename) Then bSaved = True
            End If
        End If
    Else
        If MsgBox( _
            "Are you sure you want to save? Data entered cannot be edited after saving", _
            vbYesNo, "Save?" _
        ) = vbYes Then
            If SaveSheet("") Then bSaved = True
        End If
    End If
    

    I’ve not fully tested the above, but it should give you some ideas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow up question from another post but that post was answered.
This is a follow up from a question of mine that was just answered
This is a follow on from another question i made I have this query
This is a follow up question from Problem with array assignment I now have
Note that this is a follow-up question from my previous one: How to memorize
My question is sort of a follow on from this question below but I
Hey guys. This is a follow-on from this question : After getting the right
This is a follow-on from a previous question, in the implementation, I have two
This is a follow-up to my question from yesterday . I have Scott Meyers'
This question is a follow on from the one that was asked here .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.