mardi 21 mars 2017

BeforeClose will not close my Excel-Sheet VBA

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 stirng. 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 aprpeciate 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

Aucun commentaire:

Enregistrer un commentaire