Code works but can't save template!

shazadhussain88

New Member
Joined
Oct 13, 2023
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Hopefully you guys can help a real newbie with an issue im having with a Macro.

I have a excel file which i would like to share with my team to populate. There is a field where either a Yes or No option must be filled. The default field is 'Please Select'. I have added the below code which prevents the users from saving the file without entering either Yes or No.
Looks to be working fine, however now I am unsure how to save this template to distribute to the team!, as it keeps asking me to select yes or no. I want to save it as 'please select' and share with the team who from this point must select either yes or no. hopefully this makes sense!

ANy help would be appreciated !



VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Application.ScreenUpdating = False
    Dim rng As Range
    For Each rng In Sheets("DCR").Range("E18:F18")
        If rng = "Please Select" Then
            MsgBox ("Please select 'Yes' or 'No' in cell " & rng.Address(0, 0))
            Cancel = True
            Exit For
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.ScreenUpdating = False
    Dim rng As Range
    For Each rng In Sheets("DCR").Range("E18:F18")
        If rng = "Please Select" Then
            MsgBox ("Please select 'Yes' or 'No' in cell " & rng.Address(0, 0))
            Cancel = True
            Exit For
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags in post #1 for you this time. 😊
 
Upvote 1

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Do you have the above code in the ThisWorkbook module?

Do you have 'Please Select' on sheet 'DCR' in one of the cells E18:F18 without any trailing/ leading spaces?
Hi sorry for the delay

Please see below - I can share the document with you if you would be ok with that? seriously appreciate your help with this!

1697447801917.png
 
Upvote 0
If you can share the workbok, it would be easier to look at.
 
Upvote 0
You can use something like OneDrive, the forum itself does not accept attachments.

If you use OneDrive, make sure to share the file access.
 
Upvote 0
You would also need to update the below:
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Application.UserName <> "Hussein, Shazad (S.)" Then ' change the Joe Bloggs to your username
        Application.ScreenUpdating = False
        Dim rng As Range
        For Each rng In Sheets("DCR").Range("E18:F18")
            If rng = "Please Select" Then
                MsgBox ("Please select 'Yes' or 'No' in cell " & rng.Address(0, 0))
                Cancel = True
                Exit For
            End If
        Next rng
        Application.ScreenUpdating = True
    End If
End Sub
 
Upvote 0
so ended up with the below, and I was able to save it. however when any other user opens it they seem to be able to save it without any issue?

Do they have the same username as you?
Are they enableing macro's when they open the workbook? Check if thire macro's are disabled.
 
Upvote 0
Do they have the same username as you?
Are they enableing macro's when they open the workbook? Check if thire macro's are disabled.
They have a different user name and are enabling the macros but still not working for some reason.....

Would you know a different code that I could try to acheive the same intended outcome? i.e. I just want to prevent anyone being able to save the document without answering either Yes or No

I am stumped!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top