It seems that is diffcult to do what I wish.
However, I got another idea from this excel file with some modification
http://www.ozgrid.com/FreeDownloads/PasswordBook.zip
in this file, there is a sheet called "Config" that contents all user name & password.
I would like to delete that sheet i.e. "Config" and keep two user names + password in the vba itself.
meaning, I hope you modify this code for UserForm1
Option Explicit
Private Sub CancelButt_Click()
ThisWorkbook.Close SaveChanges:=False
End Sub
Private Sub CommandButton1_Click()
ThisWorkbook.Close SaveChanges:=False
End Sub
Private Sub OKButt_Click()
Dim iFoundPass As Integer
On Error Resume Next
With Sheets("Config").Range("UserNames")
iFoundPass = .Find(What:=UserNameTextBox, After:=.Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= False).Row
End With
On Error GoTo 0
If iFoundPass = 0 Then
SomethingWrong
Exit Sub
End If
If Sheets("Config").Cells(iFoundPass, 2) <> PasswordTextBox Then
SomethingWrong
Exit Sub
End If
Sheets("Config").Range("LoggedInAs") = UserNameTextBox
Unload Me
End Sub
Private Sub PasswordTextBox_Change()
OKButt.Enabled = (UserNameTextBox.TextLength > 2 And _
PasswordTextBox.TextLength > 2)
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then Cancel = True
End Sub
Private Sub UserNameTextBox_Change()
OKButt.Enabled = (UserNameTextBox.TextLength > 2 And _
PasswordTextBox.TextLength > 2)
End Sub
Private Sub SomethingWrong()
MsgBox "Incorrect Username or Password.", vbCritical + vbInformation, "OzGrid.com"
End Sub
VBA in ThisWorkbook, I modified it to become as follow:
Option Explicit
Private Sub Workbook_Open()
Dim objNet As Object
Dim i As Integer
On Error Resume Next
Set objNet = CreateObject("WScript.NetWork")
'To select the first sheet
For i = 1 To Worksheets.Count
If Sheets(i).Visible Then
Sheets(i).Select
Exit For
End If
Next i
If objNet.UserName <> "loveguy1977" Then
Set objNet = Nothing
Sheets("Config").Visible = xlVeryHidden
Application.EnableCancelKey = xlDisabled
UserForm1.Show
End If
Set objNet = Nothing
End Sub
Then I will protect each excel file's VBA with password.
Thank you very much