Consulting

Results 1 to 4 of 4

Thread: Solved: Need to use the sheets change event to undo changes made to Cell A1

  1. #1
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location

    Solved: Need to use the sheets change event to undo changes made to Cell A1

    (1) I have Range("A1:Z9") merged. When selected, Excel treats it as being Cell A1.
    - It contains a value. Lets say that value is "My Value in Cell A1".

    (2) I also have the sheets selection change event turning sheet protection off if that merged cell is selected.

    (3) The sheet is (protected again) when any other cell is selected.
    That command is also handled in the selection change event.

    (4) --- What is the code that I can use in the Sheets change event that will undo any change that is made in that merged cell(?)- The undo routine would be triggered when the selection is moved out of the merged cell to any other cell.

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi Frank,

    I personally avoid merged cells if possible. That said, here's a try:
    [VBA]
    Option Explicit

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Address(0, 0) = "A1:Z9" Then
    Me.Unprotect
    ElseIf Not Me.ProtectContents Then
    Me.Protect , , , , True
    Me.Cells(1).Value = "My Value in Cell A1"
    End If
    End Sub
    [/VBA]
    Hope that helps,

    Mark

  3. #3
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    Hi Mark,

    It looks to me like that code sets the cells value only if that cell is selected, rather than undo-ing a change, Edit: no matter what cell is selected, as I need.

    I believe we need to use the change event.

    Thanks buddy
    Last edited by frank_m; 11-12-2011 at 06:30 PM.

  4. #4
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    Edit: This modified version of your code does what I need.
    In fact I know enough I should have thought of this approach myself.
    -- Thanks Mark.
    [vba]Option Explicit

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Me.Cells(1).Value <> "My Value in Cell A1" Then
    Me.Unprotect
    Me.Cells(1).Value = "My Value in Cell A1"
    ElseIf Not Me.ProtectContents Then
    Me.Protect , , , , True

    End If
    End Sub [/vba]

Posting Permissions

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