Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default password VBA and need for Macro

I need to protect a workbook with a password, with the password being
unique based on the user's login id. In addition, I need to prevent
the use of the workbook if macros are disabled.

I downloaded this code from http://www.ozgrid.com/download/default.htm
[passwordbook], which works fine for the password portion:

Private Sub CancelButt_Click()
ThisWorkbook.Close SaveChanges:=False
End Sub

Private Sub CommandButton1_Click()
ThisWorkbook.Close SaveChanges:=False
End Sub

Private Sub Label4_Click()

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, "SDP Business Case"
End Sub

***************

I have also added the following code, to hide all sheets except one
unless macros are enabled:

Sub On_Open()

Worksheets("Macros").Visible = xlSheetVeryHidden
Worksheets("Introduction").Visible = True


End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Worksheets("Introduction").Visible = xlSheetVeryHidden
Worksheets("Macros").Visible = True

End Sub

The problem I get, is that if macros are enabled, after the correct
password is entered, I get "Run Time Error 1004 - Unable to set the
Visible property of the worksheet class", and the sheets that were
supposed to become visible remain hidden. I'm new to VBA, but I'm
guessing that the passord code some how prevents the macros from
unhiding the sheets.

Any help would be appreciated.

Thanks.
Jason
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 550
Default password VBA and need for Macro

Jason,

First question is "how many sheets are in your workbook"
One sheet 'always' has to be visible.

If you only have two sheets then:
Worksheets("Macros").Visible = xlSheetVeryHidden
Worksheets("Introduction").Visible = True

in both the Open and Close events have to be flopped.
Make the "Introducton" visible before you try to hide the
"Macros" sheet.

John

"Jason" wrote in message
m...
I need to protect a workbook with a password, with the password being
unique based on the user's login id. In addition, I need to prevent
the use of the workbook if macros are disabled.

I downloaded this code from http://www.ozgrid.com/download/default.htm
[passwordbook], which works fine for the password portion:

Private Sub CancelButt_Click()
ThisWorkbook.Close SaveChanges:=False
End Sub

Private Sub CommandButton1_Click()
ThisWorkbook.Close SaveChanges:=False
End Sub

Private Sub Label4_Click()

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, "SDP Business Case"
End Sub

***************

I have also added the following code, to hide all sheets except one
unless macros are enabled:

Sub On_Open()

Worksheets("Macros").Visible = xlSheetVeryHidden
Worksheets("Introduction").Visible = True


End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Worksheets("Introduction").Visible = xlSheetVeryHidden
Worksheets("Macros").Visible = True

End Sub

The problem I get, is that if macros are enabled, after the correct
password is entered, I get "Run Time Error 1004 - Unable to set the
Visible property of the worksheet class", and the sheets that were
supposed to become visible remain hidden. I'm new to VBA, but I'm
guessing that the passord code some how prevents the macros from
unhiding the sheets.

Any help would be appreciated.

Thanks.
Jason



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default password VBA and need for Macro

John,

Thanks...that worked.

Jason
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Password protection in macro ( Anybody can view my password in VB Sherees Excel Discussion (Misc queries) 2 January 24th 10 10:05 PM
Macro (password) Mike Excel Discussion (Misc queries) 6 December 25th 09 01:25 PM
Macro Password Kevin Excel Discussion (Misc queries) 3 January 2nd 08 08:03 PM
Password and Macro Wanna Learn Excel Discussion (Misc queries) 4 July 24th 07 03:20 PM
How to see macro code of a password protected macro without a password? Dmitry Kopnichev Excel Worksheet Functions 5 October 27th 05 09:57 AM


All times are GMT +1. The time now is 12:27 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"