Share via

VBA - worksheet selection change two ranges

Anonymous
2018-01-29T20:03:55+00:00

I have an excel sheet that has two tables which are split by a few rows.  I input a conditional format into the cells in column C for status where the format all cells based on their values, format style is icon sets, the first icon is a green circle when value is 2 and the second icon is a red circle when <2 and 1.  No cell icon when <1.  I put the below code into VBA and it works except every cell in column C changes when I click into it.  I would like to have it only change when it is in a certain range of my two tables, so when it is between C5 and C10 and then also when it is between C15 and C20.  Please let me know if you need me to provide any additional clarifications.  Thank you.

A B C
1
2
3
4 ID Type Status
5 CT1 15
6 CT2 15 2
7 AN1 15
8 AN2 15 2
9 WG1 15
10 WG2 15 1
11
12
13
14 ID Type Status
15 CT1 10
16 CT2 10 1
17 AN1 10
18 AN2 10 1
19 WG1 10
20 WG2 10 1

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

     If Target.Column <> 3 Then Exit Sub

          On Error GoTo ErrHandler

     Application.EnableEvents = False

     With Target.Cells(1)   'use with merged cells

         If .Value = 1 Then

             .Value = 2

         Else

             .Value = 1

         End If

     End With

     Application.EnableEvents = True

     Exit Sub

ErrHandler:

     Application.EnableEvents = True

 End Sub

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

Answer accepted by question author

HansV 462.6K Reputation points
2018-01-29T20:23:23+00:00

Change

    If Target.Column <> 3 Then Exit Sub

to

    If Intersect(Target.Cells(1), Range("C5:C10,C15:C20")) Is Nothing Then Exit Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2018-01-29T20:30:29+00:00

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

        With Target.Cells(1)

            If .Row > 20 Or .Row < 5 Or (.Row > 10 And .Row < 15) Or .Column <> 3 Then Exit Sub

            On Error GoTo ErrHandler

            Application.EnableEvents = False

            If .Value = 1 Then

                .Value = 2

            Else

                .Value = 1

            End If

        End With

    ErrHandler:

        Application.EnableEvents = True

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2018-01-29T20:27:22+00:00

    Thank you for the quick reply! I appreciate your help.

    Was this answer helpful?

    0 comments No comments