+ Reply to Thread
Results 1 to 4 of 4

Unprotect and Protect sheet within macro

  1. #1
    John F
    Guest

    Unprotect and Protect sheet within macro

    The following causes "Variable Not Defined" message
    NOTE: Hide rows macro works great. I can't get the unprotect and protect to
    work
    if spite of numerous attempts at understanding user group info and the
    microsoft help. Obviously a lot to learn yet.
    Is part of the problem the positioning of the unprotect statement?
    This macro will be used for a number of worksheets/workbooks so I can't have
    the statement directly identify the worksheet name (since it is different in
    each workbook).


    Option Explicit
    Sub Hide_Rows()
    ' Hide_Rows Macro
    ' January 11, 2005
    ' From Frank Kabel
    ' Keyboard Shortcut: NONE

    Worksheet.Unprotect Password:="password"

    Dim RowNdx As Long
    Dim LastRow As Long
    Application.ScreenUpdating = False
    LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    For RowNdx = LastRow To 1 Step -1
    If Cells(RowNdx, "B").Value = "x" Then
    Rows(RowNdx).Hidden = True
    End If
    Next RowNdx
    Application.ScreenUpdating = True

    Worksheet.Protect Password:="password"

    End Sub
    --
    John F. Scholten

  2. #2
    JulieD
    Guest

    Re: Unprotect and Protect sheet within macro

    Hi John

    there's no such thing as "worksheet" - if you use "worksheets" you have to
    identify which worksheet you're talking about so that won't help you much ..
    try instead (for both the protect & unprotect)

    Activesheet.protect Password:="password"


    Cheers
    julieD


    "John F" <[email protected]> wrote in message
    news:[email protected]...
    > The following causes "Variable Not Defined" message
    > NOTE: Hide rows macro works great. I can't get the unprotect and protect
    > to
    > work
    > if spite of numerous attempts at understanding user group info and the
    > microsoft help. Obviously a lot to learn yet.
    > Is part of the problem the positioning of the unprotect statement?
    > This macro will be used for a number of worksheets/workbooks so I can't
    > have
    > the statement directly identify the worksheet name (since it is different
    > in
    > each workbook).
    >
    >
    > Option Explicit
    > Sub Hide_Rows()
    > ' Hide_Rows Macro
    > ' January 11, 2005
    > ' From Frank Kabel
    > ' Keyboard Shortcut: NONE
    >
    > Worksheet.Unprotect Password:="password"
    >
    > Dim RowNdx As Long
    > Dim LastRow As Long
    > Application.ScreenUpdating = False
    > LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    > For RowNdx = LastRow To 1 Step -1
    > If Cells(RowNdx, "B").Value = "x" Then
    > Rows(RowNdx).Hidden = True
    > End If
    > Next RowNdx
    > Application.ScreenUpdating = True
    >
    > Worksheet.Protect Password:="password"
    >
    > End Sub
    > --
    > John F. Scholten




  3. #3
    Tom Ogilvy
    Guest

    Re: Unprotect and Protect sheet within macro

    Option Explicit
    Sub Hide_Rows()
    ' Hide_Rows Macro
    ' January 11, 2005
    ' From Frank Kabel
    ' Keyboard Shortcut: NONE

    Activesheet.Unprotect Password:="password"

    Dim RowNdx As Long
    Dim LastRow As Long
    Application.ScreenUpdating = False
    LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    For RowNdx = LastRow To 1 Step -1
    If Cells(RowNdx, "B").Value = "x" Then
    Rows(RowNdx).Hidden = True
    End If
    Next RowNdx
    Application.ScreenUpdating = True

    ActiveSheet.Protect Password:="password"

    End Sub

    --
    Regards,
    Tom Ogilvy

    "John F" <[email protected]> wrote in message
    news:[email protected]...
    > The following causes "Variable Not Defined" message
    > NOTE: Hide rows macro works great. I can't get the unprotect and protect

    to
    > work
    > if spite of numerous attempts at understanding user group info and the
    > microsoft help. Obviously a lot to learn yet.
    > Is part of the problem the positioning of the unprotect statement?
    > This macro will be used for a number of worksheets/workbooks so I can't

    have
    > the statement directly identify the worksheet name (since it is different

    in
    > each workbook).
    >
    >
    > Option Explicit
    > Sub Hide_Rows()
    > ' Hide_Rows Macro
    > ' January 11, 2005
    > ' From Frank Kabel
    > ' Keyboard Shortcut: NONE
    >
    > Worksheet.Unprotect Password:="password"
    >
    > Dim RowNdx As Long
    > Dim LastRow As Long
    > Application.ScreenUpdating = False
    > LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    > For RowNdx = LastRow To 1 Step -1
    > If Cells(RowNdx, "B").Value = "x" Then
    > Rows(RowNdx).Hidden = True
    > End If
    > Next RowNdx
    > Application.ScreenUpdating = True
    >
    > Worksheet.Protect Password:="password"
    >
    > End Sub
    > --
    > John F. Scholten




  4. #4
    John F
    Guest

    Re: Unprotect and Protect sheet within macro

    Thanks so much - Works great!!
    JFS

    "Tom Ogilvy" wrote:

    > Option Explicit
    > Sub Hide_Rows()
    > ' Hide_Rows Macro
    > ' January 11, 2005
    > ' From Frank Kabel
    > ' Keyboard Shortcut: NONE
    >
    > Activesheet.Unprotect Password:="password"
    >
    > Dim RowNdx As Long
    > Dim LastRow As Long
    > Application.ScreenUpdating = False
    > LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    > For RowNdx = LastRow To 1 Step -1
    > If Cells(RowNdx, "B").Value = "x" Then
    > Rows(RowNdx).Hidden = True
    > End If
    > Next RowNdx
    > Application.ScreenUpdating = True
    >
    > ActiveSheet.Protect Password:="password"
    >
    > End Sub
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "John F" <[email protected]> wrote in message
    > news:[email protected]...
    > > The following causes "Variable Not Defined" message
    > > NOTE: Hide rows macro works great. I can't get the unprotect and protect

    > to
    > > work
    > > if spite of numerous attempts at understanding user group info and the
    > > microsoft help. Obviously a lot to learn yet.
    > > Is part of the problem the positioning of the unprotect statement?
    > > This macro will be used for a number of worksheets/workbooks so I can't

    > have
    > > the statement directly identify the worksheet name (since it is different

    > in
    > > each workbook).
    > >
    > >
    > > Option Explicit
    > > Sub Hide_Rows()
    > > ' Hide_Rows Macro
    > > ' January 11, 2005
    > > ' From Frank Kabel
    > > ' Keyboard Shortcut: NONE
    > >
    > > Worksheet.Unprotect Password:="password"
    > >
    > > Dim RowNdx As Long
    > > Dim LastRow As Long
    > > Application.ScreenUpdating = False
    > > LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    > > For RowNdx = LastRow To 1 Step -1
    > > If Cells(RowNdx, "B").Value = "x" Then
    > > Rows(RowNdx).Hidden = True
    > > End If
    > > Next RowNdx
    > > Application.ScreenUpdating = True
    > >
    > > Worksheet.Protect Password:="password"
    > >
    > > End Sub
    > > --
    > > John F. Scholten

    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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