Home » excel » BeforeClose will not close my Excel-Sheet VBA

BeforeClose will not close my Excel-Sheet VBA

Posted by: admin April 23, 2020 Leave a comment

Questions:

So I have been trying to put together a little Excel sheet, that has an entry Log in it. So whenever the sheet is closed, Name, Date and Time are added.

So basically I have three macro running, I will only mention two. The main macro will ask if I want to close the sheet and I will have to answer with yes or no. This works fine. If I press yes the main macro will call a sub macro, that will ask me to enter a string. If this Inputbox is empty or the entry is canceled, I want the main sub to stop running and cancel the Close process. Which won’t seem to work. The error in the code to me seems pretty clear, but I don’t know how to prevent it and find a better solution. If you could help me come up with a solution I would really appreciate it.

This line of code seems to be the problem:

If Cancel_Button_LOG = False Then Cancel = True

Here I will add compressed versions of the two macros

Public Sub Add_Entry_to_Log() 
    Dim i As Integer 
    Dim response As Variant

    Cancel_Button_LOG = True 
    response = InputBox("Please enter your Name", "Name") 
    If response <> "" Then
    Else        
        Cancel_Button_LOG = False
        MsgBox "Please enter your name", vbExclamation + vbOKOnly, "Name" 
    End If   
    Worksheets("Log").Protect "secret"  
    ThisWorkbook.Save 
End Sub

Now I will want to use the Cancel_Button_log Variable to cancel the main sub:

    Dim answer As Variant 
    answer = MsgBox("Are your sure you want to close the workbook?", vbYesNo) Cancel = False 
    Select Case answer
         Case Is = vbYes
             Worksheets("Log").Unprotect "secret"
             Call Test 
             Call Add_Entry_to_Log 
             If Cancel_Button_LOG = False Then Cancel = True       
             Worksheets("Log").Protect "secret"
         Case Is = vbNo
             Cancel = True 
    End Select

    ThisWorkbook.Save

End Sub
How to&Answers:

I think you’re doing this in the most complicated way possible. If I understand your requirements correctly – you can replace all your code in your ThisWorkbook module with something like this:

Const WB_LOG As String = "Log" '// name of sheet that the log is in

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If MsgBox("Do you really want to close the workbook?", vbYesNo) = vbYes Then
        With Sheets(WB_LOG)
            .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).Resize(1, 2).Value = Array(Environ$("USERNAME"), Now)
        End With

        ThisWorkbook.Save

    Else
        Cancel = True
    End If
End Sub

Private Sub Workbook_Open()
    With Sheets(WB_LOG)
        .Protect Password:="secret", UserInterfaceOnly:=True
        .Range("A1:B1").Value = Array("USERNAME", "TIMESTAMP")
    End With
End Sub

This would negate the need for the user to manually insert their name (assuming their system username would suffice*) and also negate the need to unprotect the worksheet each time as I’ve used the UserInterfaceOnly option.


* Environment variables such as %USERNAME% can be falsified if a user wishes to do so and knows how – however someone typing their name into a textbox is even easier to falsify…