Consulting

Results 1 to 7 of 7

Thread: Protecting area of worksheet

  1. #1
    VBAX Regular
    Joined
    Mar 2006
    Posts
    15
    Location

    Protecting area of worksheet

    I´m trying to protect an area of a worksheet with the code:

    Private Sub Worksheet_Activate()

    ActiveSheet.ScrollArea = "A1:A20"

    End Sub

    That works great.

    But my problem is that i want to protect A1:A20 and C1:C15 and E15:E25 in a similar way. Can I use the above code in any way to do this? Or is there a better way to do this?

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You can unlock those cells and lock all the others, then change the selection to unlocked cells only.

    [VBA]ActiveSheet.EnableSelection = xlUnlockedCells[/VBA]

  3. #3
    VBAX Regular
    Joined
    Mar 2006
    Posts
    15
    Location
    Thank you, I tried that in this way:

    [VBA]
    Private Sub Worksheet_Activate()
    With Worksheets(1)
    .EnableSelection = xlNoSelection
    .Protect Contents:=True, UserInterfaceOnly:=True
    End With
    ActiveSheet.EnableSelection = xlUnlockedCells
    End Sub
    [/VBA]

    And that works. But then my next problem comes up: I would like to highligt the column and row of the active cell with the code:

    [VBA]
    Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

    Cells.Interior.ColorIndex = xlNone
    With ActiveCell
    .EntireRow.Interior.ColorIndex = 37
    .EntireColumn.Interior.ColorIndex = 37
    .Interior.ColorIndex = 36
    End With
    End Sub
    [/VBA]

    That code works well on it's own, but not togeter with the first one

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Make sure you unlock (Format | Cells | Protection) all the cells that the user can select.

  5. #5
    VBAX Regular
    Joined
    Mar 2006
    Posts
    15
    Location
    I have done that, but all I get is an errorcode 1004, saying ColorIndex can't be used with class Interior. I can't understand why, because each sub works well on it's own, but not together....

  6. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Try the attachment.

  7. #7
    VBAX Regular
    Joined
    Mar 2006
    Posts
    15
    Location
    Thank you, that works great

Posting Permissions

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