I want to keep an Excel file open, even if a user clicks "X". I know that they could end the process from the task manager, but I do not think that will be the case.
Basically, an Excel sheet needs to be open 24/7 to acquire data in real time from another source. Someone or something is shutting down Excel once in a while and messing this up. I want to see if a little bit of simple VBA code will fix it.
From my research so far, I added this code:
Sub Workbook_BeforeClose(Cancel As Boolean)
'Step 1: Check to see if cell C7 is blank
If Sheets("Sheet1").Range("A1").Value = "" Then
'Step 2: If cell is blank, cancel the close and tell user
Cancel = True
MsgBox "Cell A1 cannot be blank"
'Step 3: If cell is not blank, save and close
Else
ActiveWorkbook.Close SaveChanges:=True
End If
End Sub
And it works the first time I click X! But then, I just click it again and it lets me close everything without any message displaying. P.S. I have also tried this with just "cancel = True" in the sub. This also did not work.
What am I doing wrong here? Thanks a lot guys!