Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disallow Save unless in specific Macro (Excel)

Status
Not open for further replies.

RP1America

Technical User
Aug 17, 2009
221
0
0
US
I have a template workbook that must be saved by initiating an on-sheet command button. I have code below that disallows the user to save via the normal channels, yet still allows me (my user id) to do so to make updates to the original document.

However, I DO need the user to be able to save via coding within the command button's macro.

Thoughts on how to accomplish this?

ThisWorkbook
Code:
Public Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    If Environ("username") = "n264rsp" Then
    bSave = True
    Else
    bSave = False
    End If
    
    If Not bSave Then
    Cancel = True
    MsgBox "You must use the Save & Exit command button on the Dashboard Sheet"
    End If
    
End Sub

Command Button Macro
Code:
Option Explicit

Public bSave As Boolean


Public Sub SaveExit()

    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs "K:\GA2PT - Group Annuity to Trust Conversions\Current Conversion Checklists\" _
    & strPlan & " - GA2PT Conversion.xlsm", FileFormat:=52
    Application.DisplayAlerts = True
    

End Sub
 


hi,

You could assign bSave in your SaveExit procedure and then in the event, code around bSave to exclude the environ stuff.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Something like this?

ThisWorkbook
Code:
Public Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
If bSave = False Then
    If Environ("username") = "n264rsp" Then
    bSave = True
    Else
    bSave = False
    End If
    
    If Not bSave Then
    Cancel = True
    MsgBox "You must use the Save & Exit command button on the Dashboard Sheet"
    End If
End If

End Sub

Command Button Macro
Code:
Option Explicit

Public bSave As Boolean


Public Sub SaveExit()

    Application.DisplayAlerts = False
    bSave = True
    ActiveWorkbook.SaveAs "K:\GA2PT - Group Annuity to Trust Conversions\Current Conversion Checklists\" _
    & strPlan & " - GA2PT Conversion.xlsm", FileFormat:=52
    Application.DisplayAlerts = True
    

End Sub
 
Code:
Public Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Environ("username") <> "n264rsp" And Not bSave Then
    Cancel = True
    MsgBox "You must use the Save & Exit command button on the Dashboard Sheet"
End If
End Sub


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top