+ Reply to Thread
Results 1 to 2 of 2

Excel 2007 : Run time error :1004 while protecting the workbook

  1. #1
    Registered User
    Join Date
    07-31-2011
    Location
    Bhutan
    MS-Off Ver
    Excel 2007
    Posts
    16

    Unhappy Run time error :1004 while protecting the workbook

    Dear all,
    I am a teacher and learning excel 2007. I have developed the result sheet for the students for 30 sections in 2 class. I want the respective class teacher to view only the excel sheets pertaining to their section. For this, I have created a log in form using macros and it is running fine.
    BUT when i wanted to protect the workbook (structure) with a password so that users don't add or delete sheets, the error pops up " Run time error'1004' unable to set the visible property of the worksheet class.
    When i debug the error, it is with the sheets-Users and Splash. but if I chage the codes, i don't get the correct result as before the protection. Below is the attached codes for both the workbook and login form. Please help me if the workbook structure could be protected with the above points as reference.
    Thank you for you genuine support in advance.

    BELOW IS THE CODE FOR THE WORKSHEET

    Private bBkIsClose As Boolean
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    bBkIsClose = True
    End Sub

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim wsArray() As Variant
    Dim iCnt As Integer
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    ThisWorkbook.Save
    Application.EnableEvents = True
    Worksheets("Splash").Visible = True
    Worksheets("Users").Visible = True
    For Each wsSht In ThisWorkbook.Worksheets
    If Not wsSht.Name = "Splash" Then
    wsSht.Visible = xlSheetVeryHidden
    iCnt = iCnt + 1: ReDim Preserve wsArray(1 To iCnt)
    wsArray(iCnt) = wsSht.Name
    End If
    Next
    Application.EnableEvents = True
    If Not bBkIsClose Then
    For iCnt = 1 To UBound(wsArray)
    Worksheets(wsArray(iCnt)).Visible = True
    Next iCnt
    Worksheets("Splash").Visible = False
    Worksheets("Users").Visible = Fasle
    Cancel = True
    End If
    Application.ScreenUpdating = False
    End Sub
    Private Sub Workbook_Open()
    Dim wsSht As Worksheet
    Dim bar As CommandBar
    Debug.Print Application.CommandBars.Count
    For Each bar In Application.CommandBars
    Select Case bar.Name
    Case Is = "MyToolbar"
    'do nothing
    Case Is = "MyOtherToolBar"
    'do nothing
    Case Else
    bar.Enabled = True
    End Select
    Next bar
    ActiveWindow.DisplayWorkbookTabs = True
    Worksheets("Splash").Visible = True
    Worksheets("Users").Visible = Fasle
    For Each wsSht In ThisWorkbook.Worksheets
    If Not wsSht.Name = "Splash" Then wsSht.Visible = xlSheetVeryHidden
    Next wsSht
    With Worksheets("Splash")
    .Visible = True
    .Activate
    End With
    Application.EnableEvents = True
    frmLogin.Show
    bBkIsClose = True

    End Sub




    BELOW IS THE CODE FOR THE LOG IN FORM


    Option Explicit
    Dim iCounta As Integer
    Private Sub cmbValidate_Click()
    Dim rUsers As Range, rPasses As Range, rSheets As Range
    Dim lUserRow As Long
    Dim wsName As Variant
    Dim wsArray As Variant
    With Worksheets("Users").UsedRange
    Set rUsers = .Columns(1)
    Set rPasses = .Columns(2)
    Set rSheets = .Columns(3)
    End With
    If Application.WorksheetFunction.CountIf(rUsers, Me.ComboBox1.Value) < 1 Or Me.ComboBox1.Value = "" Then
    MsgBox "Invalid Username", vbExclamation, "Alert"
    ComboBox1.SetFocus
    Else
    lUserRow = Application.WorksheetFunction.Match(Me.ComboBox1.Value, rUsers, False)
    If Not CStr(Me.tbxPW.Value) = CStr(rPasses.Rows(lUserRow).Value) Or Me.tbxPW.Value = "" Then
    MsgBox "Invalid Password", vbExclamation, "Alert"
    tbxPW.SetFocus

    Else
    UnlockSheets (rSheets.Rows(lUserRow).Value)
    Unload Me
    End
    End If
    End If
    With Me
    .LblTries.Caption = .LblTries.Caption - 1
    ComboBox1.Value = vbNullString
    tbxPW.Value = vbNullString
    ComboBox1.SetFocus
    End With
    iCounta = iCounta + 1
    If iCounta > 2 Then
    MsgBox "3 Invalid Attempts. WorkBook Will Now Close", vbOKOnly + vbCritical, "Warning"
    ActiveWorkbook.Close SaveChanges:=False
    End If
    End Sub
    Private Sub CommandButton1_Click()
    ActiveWorkbook.Close SaveChanges:=False

    End Sub

    Private Sub Label7_Click()

    End Sub

    Private Sub Label4_Click()

    End Sub

    Private Sub tbxPW_Change()
    cmbValidate.Enabled = (ComboBox1.TextLength > 3 And _
    tbxPW.TextLength > 3)
    End Sub

    Private Function UnlockSheets(sShts As Variant)
    Dim wsArray As Variant
    Dim iLoop As Integer
    On Error GoTo BadShts
    If sShts = "" Then GoTo BadShts
    If InStr(1, sShts, ",") > 0 Then
    wsArray = Split(sShts, ",")
    For iLoop = 0 To UBound(wsArray)
    Worksheets(wsArray(iLoop)).Visible = True
    Next iLoop
    Else
    Worksheets(sShts).Visible = True
    End If
    Worksheets("Splash").Visible = True
    MsgBox "DEAR TEACHER! Please READ these INSTRUCTIONS.The Result Processing System is Designed for 7 Sections with a Maximum of 50 Students in Each Section.You just have to feed the information in the StudentInfo Form marked with Yellow Colours only. After you have entered the information Save the file for Mid-Term and Annual Yearwise,eg-MT-2011,AN-2011 for not losing the data of the previous years.Finally,Don't Add or Delete any Sheets.Else your system will not function."
    On Error GoTo 0
    Exit Function
    BadShts:
    MsgBox "Invalid Sheet Names", vbCritical
    End Function
    Private Sub UserForm_Initialize()
    Me.LblTries.Caption = 3
    'sets source of combobox1
    Dim myrow As Long
    Dim myrange As String
    Sheets("Users").Visible = True
    myrow = Sheets("Users").Range("A65536").End(xlUp).Row
    myrange = "Users!A2:E" & myrow
    Me.ComboBox1.RowSource = myrange
    End Sub
    Private Sub UserForm_QueryClose _
    (Cancel As Integer, CloseMode As Integer)
    ' Prevents use of the Close button
    If CloseMode = vbFormControlMenu Then
    MsgBox "Quit Now?", vbOKOnly + vbCritical, "Warning"
    ActiveWorkbook.Close SaveChanges:=False
    Cancel = True
    End If

    End Sub

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Run time error :1004 while protecting the workbook

    Hello and welcome to the forum - unfortunately you need to read the forum rules and use code tags, especially when posting such a large amount of code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1