Consulting

Results 1 to 4 of 4

Thread: Selecting 2 Cells to the right of the ActiveCell

  1. #1
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location

    Selecting 2 Cells to the right of the ActiveCell

    I have a selection based on a data validation where if the value is "O", i want to select 2 cells to the right and protect it else unprotect it.

    [VBA]
    Private Sub Worksheet_Change(ByVal Target As Range)

    Application.ScreenUpdating = False

    If ActiveCell.Value = "O" Then 'is a data validation selection

    'How to make below statement in one statement
    ActiveCell.Offset(0, 1).Select
    ActiveCell.ClearContents
    ActiveCell.Offset(0, 1).Select
    ActiveCell.ClearContents
    'I want to protect the 2 off set cell
    Else

    'I want to unprotect the 2 off set cell
    End If
    Application.ScreenUpdating = True

    End Sub


    [/VBA]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  2. #2
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Try:

    [VBA]Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Dim r As Range
    Set r = ActiveCell.Offset(0, 1).Resize(1, 2)
    If ActiveCell.Value = "O" Then
    r.ClearContents
    r.Locked = True
    Else
    r.Locked = False
    End If
    Application.ScreenUpdating = True
    End Sub
    [/VBA]

    Be aware that you are referencing ActiveCell rather than Target.
    Have a Great Day!

  3. #3
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Yes I am aware that I am referencing active cell because i dont know where to have a data validation event.

    I was having error on this

    r.Locked = True

    Quote Originally Posted by GarysStudent
    Try:

    [VBA]Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Dim r As Range
    Set r = ActiveCell.Offset(0, 1).Resize(1, 2)
    If ActiveCell.Value = "O" Then
    r.ClearContents
    r.Locked = True
    Else
    r.Locked = False
    End If
    Application.ScreenUpdating = True
    End Sub
    [/VBA]

    Be aware that you are referencing ActiveCell rather than Target.
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  4. #4
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    If your worksheet is protected, Excel might not let you change the Locked/Unlocked property of a cell. If you need the sheet protected, temporarily un-protect it to change the Locked status.
    Have a Great Day!

Posting Permissions

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