Consulting

Results 1 to 12 of 12

Thread: Solved: Password Protecting particular rows in excel

  1. #1
    VBAX Regular tools's Avatar
    Joined
    Apr 2008
    Posts
    70
    Location

    Solved: Password Protecting particular rows in excel

    Is there a way to password protect certain rows in an excel sheet.
    hmm for eg when I open my excel sheet the user is displayed with few rows and I dont want the user to delete those rows.
    Regards

    If I look its because i'm

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    You could hide those rows, unlock the visible cells, and password protect the worksheet.

  3. #3
    VBAX Regular tools's Avatar
    Joined
    Apr 2008
    Posts
    70
    Location
    The rows should be visible to the user so cant hide them and locking the cells works to a certain extent but the user can still unprotect the sheet and work.

    I want to allow the user to add new rows but dont want him to delete the existing rows.
    Regards

    If I look its because i'm

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    You could try adapting this kb entry
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    I'm not quite sure I follow the issue here...

    By default, all cells in the Excel sheet are locked, which takes effect when you protect the sheet. So why can't you select all the cells on the sheet, unlock them, then lock just the rows you want protected? At that point, when you protect the sheet, your critical rows are both locked and visible, and the rest of the sheet is editable.

    In addition, depending on your version of Excel, you can deal with the protection in more granular levels. In Excel 2003 (at a minimum) you can allow the user to use autofilters and insert rows, for example. I'm not sure which version of Excel you are using, but you may not need to reach to VBA for this.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  6. #6
    VBAX Regular tools's Avatar
    Joined
    Apr 2008
    Posts
    70
    Location
    The rows displayed in the excel sheet are obtained dynamically so vba is the only option available for locking the rows.

    I am using excel 2003.
    Regards

    If I look its because i'm

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Okay, fair enough.

    If you are comfortable enough with using Excel's default worksheet level password, you could do what I suggested as follows:

    [vba] Dim pw As String

    pw = "Password"

    With ActiveSheet
    .Unprotect pw
    .Cells.Locked = False
    .Rows("1:6").Cells.Locked = True

    .Protect Password:=pw, _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=True, _
    AllowFormattingColumns:=True, _
    AllowFormattingRows:=True, _
    AllowInsertingColumns:=True, _
    AllowInsertingRows:=True, _
    AllowDeletingColumns:=True, _
    AllowDeletingRows:=True, _
    AllowSorting:=True, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=True
    End With[/vba]

    You'd need to change the rows to work dynamically, and set a more complex password, of course. This example also gives you a look at all of the excel sheet protection features that can be set in 2003.

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  8. #8
    VBAX Regular tools's Avatar
    Joined
    Apr 2008
    Posts
    70
    Location
    Thanks ken

    This worked fine but it asks me for a password when I try inserting some values in a new row.
    Regards

    If I look its because i'm

  9. #9
    VBAX Regular tools's Avatar
    Joined
    Apr 2008
    Posts
    70
    Location
    My mistake i wanted to lock hmm for eg cells B15 to B20

    what i did was
    [vba]
    temp = 20
    For t = 15 to temp

    Dim pw As String
    pw = "Password"
    With ActiveSheet
    .Unprotect pw
    .Cells.Locked = False
    .Range("B" & t).Cells.Locked = True
    .Protect Password:=pw, _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=True, _
    AllowFormattingColumns:=True, _
    AllowFormattingRows:=True, _
    AllowInsertingColumns:=True, _
    AllowInsertingRows:=True, _
    AllowDeletingColumns:=True, _
    AllowDeletingRows:=True, _
    AllowSorting:=True, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=True

    End With

    Next
    [/vba]
    This code locked the last cell i.e. B20

    I tried something like this

    [VBA]
    .Range("B" & t & ":B" & temp).Cells.Locked = True
    [/VBA]

    but didnt work
    Regards

    If I look its because i'm

  10. #10
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi,

    To do cells B15:B20, you'd do something like the following:
    [vba] Dim pw As String
    Dim lRowFrom As Long
    Dim lRowTo As Long

    lRowFrom = 15
    lRowTo = 20

    pw = "Password"
    With ActiveSheet
    .Unprotect pw
    .Cells.Locked = False
    .Range("B" & lRowFrom & ":B" & lRowTo).Cells.Locked = True
    .Protect Password:=pw, _
    DrawingObjects:=False, _
    Contents:=True, _
    Scenarios:=False, _
    AllowFormattingCells:=True, _
    AllowFormattingColumns:=True, _
    AllowFormattingRows:=True, _
    AllowInsertingColumns:=True, _
    AllowInsertingRows:=True, _
    AllowDeletingColumns:=True, _
    AllowDeletingRows:=True, _
    AllowSorting:=True, _
    AllowFiltering:=True, _
    AllowUsingPivotTables:=True

    End With[/vba]

    If you want to do the whole rows, change

    [vba].Range("B" & lRowFrom & ":B" & lRowTo).Cells.Locked = True[/vba]
    To this
    [vba].Rows(lRowFrom & ":" & lRowTo).Cells.Locked = True[/vba]

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  11. #11
    VBAX Regular tools's Avatar
    Joined
    Apr 2008
    Posts
    70
    Location
    Thanks Ken

    worked fine
    Regards

    If I look its because i'm

  12. #12
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Great, glad to hear it.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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