+ Reply to Thread
Results 1 to 5 of 5

Unprotecting sheet after running a macro

Hybrid View

  1. #1
    Registered User
    Join Date
    09-20-2012
    Location
    Australia
    MS-Off Ver
    Excel 2011
    Posts
    18

    Unprotecting sheet after running a macro

    Hi Guys,

    I have this macro msgbox running in my protected sheet, by the time I select "Cancel" on the message box, my sheet become unprotected.

    I'd been done a few test, and my sheet just be unprotected just when I select "Cancel" on the macro message box.

    Could you help me to fix this problem?

    This is my macro:

    
    
    Sub AddDeleteRowStaff()
       ActiveSheet.Unprotect Password:="150480"
    
    Dim MyInput As Integer
    MyInput = Application.InputBox(Prompt:="Would you like to Add or Delete last row? " & vbNewLine _
                                           & "Enter 1 to Add row" & vbNewLine & "Enter 2 to Delete last row", _
                                   Title:="Add or Delete last row", _
                                   Default:=1, _
                                   Type:=1)
    If MyInput = 0 Then Exit Sub
    With ActiveSheet.ListObjects(1)
        Select Case MyInput
            Case 1
                MsgBox "Adding row"
                Range("A8").End(xlDown).ListObject.ListRows.Add AlwaysInsert:=False
                '.ListRows.Add
            Case 2
                MsgBox "Deleting last row"
                Range("A8").End(xlDown).ListObject.ListRows(.ListRows.Count).Delete
                '.ListRows(.ListRows.Count).Delete
        End Select
    End With
    
        
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="150480"
    
    End Sub
    Last edited by Leith Ross; 09-24-2012 at 02:33 AM. Reason: Added Code Tags

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Unprotecting sheet after running a macro

    Hi, Michell Feitosa,

    please use Code-Tags for displaying your code.

    Move the line to unprotect after the checking of the input like this:
    Sub AddDeleteRowStaff()
    
    Dim MyInput As Integer
    MyInput = Application.InputBox(Prompt:="Would you like to Add or Delete last row? " & vbNewLine _
      & "Enter 1 to Add row" & vbNewLine & "Enter 2 to Delete last row", _
      Title:="Add or Delete last row", _
      Default:=1, _
      Type:=1)
    If MyInput = 0 Then Exit Sub
    ActiveSheet.Unprotect Password:="150480"
    With ActiveSheet.ListObjects(1)
      Select Case MyInput
        Case 1
          MsgBox "Adding row"
          Range("A8").End(xlDown).ListObject.ListRows.Add AlwaysInsert:=False
          '.ListRows.Add
        Case 2
          MsgBox "Deleting last row"
          Range("A8").End(xlDown).ListObject.ListRows(.ListRows.Count).Delete
          '.ListRows(.ListRows.Count).Delete
      End Select
    End With
    
    
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="150480"
    
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    09-20-2012
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Unprotecting sheet after running a macro

    Try this:

    Sub AddDeleteRowStaff()
        ActiveSheet.Unprotect Password:="150480"
    
        Dim MyInput As String
        
        MyInput = Application.InputBox(Prompt:="Would you like to Add or Delete last row? " & vbNewLine _
            & "Enter 1 to Add row" & vbNewLine & "Enter 2 to Delete last row", _
            Title:="Add or Delete last row", _
            Default:=1, _
            Type:=1)
            
        If MyInput = "0" Or MyInput = "" Then GoTo AddDeleteRowStaff_Exit
        
        With ActiveSheet.ListObjects(1)
            Select Case MyInput
                Case 1
                    MsgBox "Adding row"
                    Range("A8").End(xlDown).ListObject.ListRows.Add AlwaysInsert:=False
                    '.ListRows.Add
                Case 2
                    MsgBox "Deleting last row"
                    Range("A8").End(xlDown).ListObject.ListRows(.ListRows.Count).Delete
                    '.ListRows(.ListRows.Count).Delete
            End Select
        End With
        
    AddDeleteRowStaff_Exit:
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="150480"
        Exit Sub
    
    End Sub
    Next time use CODE tags, or we will smash ya up! (Not really, probably just tell you to use code tags again!)

  4. #4
    Registered User
    Join Date
    09-20-2012
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Unprotecting sheet after running a macro

    Holger's is better, use his!

  5. #5
    Registered User
    Join Date
    09-20-2012
    Location
    Australia
    MS-Off Ver
    Excel 2011
    Posts
    18

    Re: Unprotecting sheet after running a macro

    You are amazing! thank you so much

+ 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