1

The below code works and changes the offset cells when north is entered, i would like to also have it change if its south, west or east but i can seem to find a way to add this.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range


Set KeyCells = Range("A7:A26")
Set rng = Range("A7:A26")

If Not Application.Intersect(KeyCells, Range(Target.Address)) _
       Is Nothing Then

    For Each cell In rng.Cells
        If cell.Value = "North" Then
            cell.Offset(0, 1).Interior.Color = RGB(0, 255, 0)
            cell.Offset(0, 2).Interior.Color = RGB(0, 255, 0)
            cell.Offset(0, 3).Interior.Color = RGB(0, 255, 0)
        End If
    Next

   End If


End Sub
1
  • Do you want to use the same colour or different colours for each direction?? Commented Apr 17, 2015 at 12:55

3 Answers 3

2

Try using a Select Case instead of If statements for this.

For Each cell In Rng.Cells
    Select Case cell.Value
        Case "North"
            Range(cell.Offset(0, 1), cell.Offset(0, 3)).Interior.Color = RGB(0, 255, 0)
        Case "South"
            Range(cell.Offset(0, 1), cell.Offset(0, 3)).Interior.Color = RGB(0, 155, 0)
        Case "East"
            Range(cell.Offset(0, 1), cell.Offset(0, 3)).Interior.Color = RGB(0, 55, 0)
        Case "West"
            Range(cell.Offset(0, 1), cell.Offset(0, 3)).Interior.Color = RGB(0, 0, 0)
    End Select
Next
Sign up to request clarification or add additional context in comments.

3 Comments

Never used Select Case before, I will have a look into it. Thanks!
Select Case is often ignored by new programmers, but it can make code easier to follow and understand. Enjoy!
1

Give this a shot. Just update the color assignments as needed:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range

Set KeyCells = Range("A7:A26")
Set rng = Range("A7:A26")

If Not Application.Intersect(KeyCells, Range(Target.Address)) _
   Is Nothing Then
    For Each cell In rng.Cells
        If cell.Value = "North" Then
            cell.Offset(0, 1).Interior.Color = RGB(0, 255, 0)
            cell.Offset(0, 2).Interior.Color = RGB(0, 255, 0)
            cell.Offset(0, 3).Interior.Color = RGB(0, 255, 0)
        ElseIf cell.Value = "South" Then
            cell.Offset(0, 1).Interior.Color = RGB(255, 0, 0)
            cell.Offset(0, 2).Interior.Color = RGB(255, 0, 0)
            cell.Offset(0, 3).Interior.Color = RGB(255, 0, 0)
        ElseIf cell.Value = "East" Then
            cell.Offset(0, 1).Interior.Color = RGB(0, 0, 255)
            cell.Offset(0, 2).Interior.Color = RGB(0, 0, 255)
            cell.Offset(0, 3).Interior.Color = RGB(0, 0, 255)
        ElseIf cell.Value = "West" Then
            cell.Offset(0, 1).Interior.Color = RGB(0, 255, 255)
            cell.Offset(0, 2).Interior.Color = RGB(0, 255, 255)
            cell.Offset(0, 3).Interior.Color = RGB(0, 255, 255)
        End If
    Next
End If
End Sub

Comments

0

Consider:

 If cell.Value = "North" Or cell.Value = "South" Then
        cell.Offset(0, 1).Interior.Color = RGB(0, 255, 0)
        cell.Offset(0, 2).Interior.Color = RGB(0, 255, 0)
        cell.Offset(0, 3).Interior.Color = RGB(0, 255, 0)
 End If

or:

 If cell.Value = "North" Then
        cell.Offset(0, 1).Interior.Color = RGB(0, 255, 0)
        cell.Offset(0, 2).Interior.Color = RGB(0, 255, 0)
        cell.Offset(0, 3).Interior.Color = RGB(0, 255, 0)
 ElseIf cell.Value = "South"
        cell.Offset(0, 1).Interior.Color = RGB(0, 0, 255)
        cell.Offset(0, 2).Interior.Color = RGB(0, 0, 255)
        cell.Offset(0, 3).Interior.Color = RGB(0, 0, 255)
 End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.