+ Reply to Thread
Results 1 to 2 of 2

Inserting rows in protected sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    06-27-2005
    Posts
    7

    Inserting rows in protected sheet

    This problem may sounds funny.
    I have two columns say Col. E and F which are protected and contains formulas.
    When I try to insert a row, its is throwing error. I want to insert a row and also the formulae in the columns in E and F has to be reflected in the inserted row.
    In the meantime the cells in the Col. E and F has to be protected.

    Is there any way for doing this?

    Thanks in advance.

    Rajesh

  2. #2
    Registered User
    Join Date
    06-29-2005
    Location
    England
    Posts
    50
    All worksheets should be protected from the prying fingers of end users, but this causes a problem when you want to programatically make changes.

    What I do is I have two subroutines Lock_Sheet and Unlock_Sheet:

    '***************************************************************************
    '*
    '* Sub:      Lock_Sheet
    '*
    '* Purpose:  Locks the sheet for changes
    '*
    '* Entry:
    '*
    '* Exit:
    '*
    '* Calls:
    '*
    '***************************************************************************
    '*
    Sub Lock_Sheet()
        StackPtr = StackPtr - 1
        Call Set_Changing(Change_Stack(StackPtr).Changing)
        Application.ScreenUpdating = Change_Stack(StackPtr).Update
        Worksheets(Change_Stack(StackPtr).Sheet_Name).Protect DrawingObjects:=Change_Stack(StackPtr).Update, Contents:=Change_Stack(StackPtr).Update, Scenarios:=Change_Stack(StackPtr).Update
        Application.EnableEvents = Change_Stack(StackPtr).Update
        Application.DisplayAlerts = Change_Stack(StackPtr).Update
        If Change_Stack(StackPtr).Update Then
           Application.Calculation = xlCalculationAutomatic
        Else
           Application.Calculation = xlCalculationManual
        End If
        Call Pop_Status
    End Sub
    
    '*
    '***************************************************************************
    '*
    '* Sub:      Unlock_Sheet
    '*
    '* Purpose:  Flags the fact that the sheet is being changed programatically
    '*           so that events do not get triggered by the changes
    '*
    '* Entry:    Name of sheet to unlock or blank for current sheet
    '*
    '* Exit:
    '*
    '* Calls:
    '*
    '***************************************************************************
    '*
    Sub Unlock_Sheet(ByVal Sheet_Name As String)
        Change_Stack(StackPtr).Sheet_Name = IIf(Sheet_Name = "", ActiveSheet.Name, Sheet_Name)
        Change_Stack(StackPtr).Changing = Changing()
        Change_Stack(StackPtr).Update = Application.ScreenUpdating
        StackPtr = StackPtr + 1
        Call Set_Changing(True)
        If Sheet_Name = "" Then
           ActiveSheet.Unprotect
        Else
           Worksheets(Sheet_Name).Unprotect
        End If
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        Call Push_Status("Busy")
    End Sub
    The actual statement that does the work is

    Worksheets(Change_Stack(StackPtr).Sheet_Name).Protect DrawingObjects:=Change_Stack(StackPtr).Update, Contents:=Change_Stack(StackPtr).Update, Scenarios:=Change_Stack(StackPtr).Update
    and

    Worksheets(Sheet_Name).Unprotect
    However, there are a lot of other things to consider as well such as:
    • Process events
    • Process Calculations
    • Display changes as they happen or not
    • Display alerts that might be triggered or not

    That's why there is a hell of a lot more code surrounding the relevant statements above.

    I tend to use a push down stack to store everything that I need to keep track of such as the current state of the worksheet, messages being displayed to the user etc. In that way I can call these routines as many times as I like as long as the calls happen in pairs.

    An example of how to do this is below:

        Call Unlock_Sheet("")
        ActiveSheet.Bottom_F = ActiveSheet.Top_B + Val(ActiveSheet.Thickness)
        Call Lock_Sheet
    Regards

    Rich
    Last edited by Rich_z; 06-29-2005 at 11:30 AM.

+ 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