In the Workbook_Open event I am running code to check the serial number to ensure it is matching data I am pulling from another data source. Then I am trying to close the file if it does not match.
When only one file is open the file will close as I would like it to. But when multiple files are open the file doesn't close.
Here is the code I am using. I only run into an issue that the file doesn't close if there are other files open.
Private Sub Workbook_Open()
Dim wb As Workbook: Set wb = ThisWorkbook
Application.ScreenUpdating = False
'Pull User_Name
Range("U_Nm").Value = Application.UserName
'Pull Serial Number from Computer
Dim fsObj As Object
Dim drv As Object
Set fsObj = CreateObject("Scripting.FileSystemObject")
Set drv = fsObj.Drives("C")
Range("SN").Value = Left(Hex(drv.SerialNumber), 4) _
& "-" & Right(Hex(drv.SerialNumber), 4)
'Sheets("Security").Visible = xlSheetVeryHidden
Sheets("Security").Visible = True
If Range("Full_Validation").Value <> "Valid" Then
'you must be connected to the internet to proceed
Sheets("Security").Range("A1").ListObject.QueryTable.Refresh BackgroundQuery:=False
If Range("User_Validation").Value <> "Valid" Then
MsgBox "Not a Valid User!"
'*******THIS LINE OF CODE IS NOT WORK WHEN I HAVE A SECOND FILE OPEN AT THE SAME TIME********
wb.Close SaveChanges:=False
ElseIf Range("Expiry_Validation").Value <> "Valid" Then
Set objShell = CreateObject("Wscript.Shell")
intMessage = MsgBox("Your Investment Calculator Subscription has Expired!" & vbCr _
& vbCr _
& "You will need to renew your subscription to the Investment Calculator." & vbCr _
& vbCr _
& "Would you like to renew your subscription?", _
vbYesNo, "There is a problem...")
If intMessage = vbYes Then
objShell.Run ("https://google.com")
Else
'Wscript.Quit
End If
ThisWorkbook.Close SaveChanges:=False
ElseIf Range("SN_Validation").Value <> "Valid" Then
MsgBox "You must first register your computer with Doug & Mark!"
ThisWorkbook.Close SaveChanges:=False
End If
'Sheets("Security").VeryHidden = True
Application.ScreenUpdating = True
End If
Range("U_Nm").ClearContents
Range("SN").ClearContents
End Sub
I would expect the file to close whether there is 1 or many files open at the time.
ThisWorkbook.Close SaveChanges:=False
should work, no matter how many files are open. Something else is wrong. Could be that some other file have a Macro that have setApplication.EnableEvents = False
. Does it give you an error, or nothing happens ? – Mikku