write at exceltoexplore@gmail.com : Report Automation|Dashboard in Excel| Provide Excel consulting through macro (VBA) automation |Financial Modeling | Ethical Hacking

Thursday, 16 June 2011

Killing Excel File If wrong Password Entered

With this code, when you open the workbook you will get inputbox
if you enter wrong password then entire workbook will be deleted.
steps :
1. In Excel Workbook for which you want to set password
2. Go to developer Tab else Press Alt + F11(http://msdn.microsoft.com/en-us/library/bb608625.aspx)
3.Click on Visual Basic icon
4. Go to view click on project Explorer else precc Ctrl + R
5.Right click on this workbook
6. click on view code
7.Copy below code in Thisworkbook module

Private Sub Workbook_Open()
Dim strPwd As String
strPwd = Application.InputBox("Enter password")
'Enter your password here
If strPwd <> "Mahesh123" Then
Application.DisplayAlerts = False
ThisWorkbook.ChangeFileAccess xlReadOnly
Kill ThisWorkbook.FullName
ThisWorkbook.Close False
End If
End Sub

No comments:

Post a Comment