If StrComp(currentUser, allowedUser, vbTextCompare) <> 0 Then MsgBox "このファイルを開く権限がありません。", vbCritical ThisWorkbook.Close SaveChanges:=False End If End Sub
' マクロ有効時に警告シートを非表示 On Error Resume Next Sheets("Sheet1").Visible = xlSheetVeryHidden On Error GoTo 0
' 認証失敗なら即閉じる If StrComp(currentUser, allowedUser, vbTextCompare) <> 0 Then MsgBox "このファイルを開く権限がありません。", vbCritical ThisWorkbook.Close SaveChanges:=False End If End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean) ' 閉じる前に警告シートを再表示(マクロ無効時に警告を残すため) On Error Resume Next Sheets("Sheet1").Visible = xlSheetVisible On Error GoTo 0 End Sub