Making Target.Address work after Iinserting a Line

Anonymous
2021-01-14T16:43:16+00:00

I am using:

If Target.Address = "$C$56" Then

    Range("C56").Value = "X"

    Range("D56").Value = ""

    Range("E56").Value = ""

ElseIf Target.Address = "$D$56" Then

    Range("C56").Value = ""

    Range("D56").Value = "X"

    Range("E56").Value = ""

ElseIf Target.Address = "$E$56" Then

    Range("C56").Value = ""

    Range("D56").Value = ""

    Range("E56").Value = "X"

in a VBA subroutine.  This works find until I insert a line in the associated spreadsheet.  The addresses in VBA do not update to the added line and the code stops working.

I have read about assigning a named range and somehow referencing the various cells within the range as a way to fix this issue, but no matter what I have tried, it does not work.  Can anyone help?

Thanks,

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2021-01-14T22:32:15+00:00

    Leo:

    Ref, "... I do not know what If Not Application.Intersect(Target, myRange) Is Nothing Then does.  Could you explain?"

    It means if the active cell (target) is within the range i.e C56:E59 then...do whatever

    The following macro will work as per your requirements

    ''''*************************************************************************************

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim myRange As Range

    Set myRange = Range("Metal_Options")

    ''' If the active cell is within the myRange

    If Not Application.Intersect(Target, myRange) Is Nothing Then

        If Target.Cells.Count > 1 Then Exit Sub  '' Select only one cell

            Range("C" & Target.Row & ":E" & Target.Row).Value = ""   ''' Clear the values

            Target.Value = "X"   ''' Insert X in the active cell

    End If

    End Sub

    '''***********************************************************************************

    Regards

    1 person found this answer helpful.
    0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-01-14T19:58:35+00:00

    HansV:

    Is there a way to select an array of cells and then specify individual cells within the range?  See my response to Jeovany.

    Thanks,

    0 comments No comments
  2. HansV 462.4K Reputation points MVP Volunteer Moderator
    2021-01-14T20:41:42+00:00

    Intersect(Target, myRange) is the range of cells that is both in Target (the selected range) and in myRange.

    If Target is completely disjunct from myRange, Intersect(Target, myRange) is Nothing (empty). So the lines between

    If Not Application.Intersect(Target, myRange) Is Nothing Then

    and

    End If

    will only be executed if Target overlaps with myRange.

    0 comments No comments
  3. Anonymous
    2021-01-15T02:16:32+00:00

    Jeovany:

    This worked great!  And so simple (after I understood what you were doing).

    Thank you so much.

    0 comments No comments