A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Change
If Target.Column <> 3 Then Exit Sub
to
If Intersect(Target.Cells(1), Range("C5:C10,C15:C20")) Is Nothing Then Exit Sub
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
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.
Answer accepted by question author
Change
If Target.Column <> 3 Then Exit Sub
to
If Intersect(Target.Cells(1), Range("C5:C10,C15:C20")) Is Nothing Then Exit Sub
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
Thank you for the quick reply! I appreciate your help.