2015-04-17 99 views
1

下面的代码工作,并更改北进入时的偏移单元格,我也想让它改变,如果它的南,西或东,但我似乎可以找到一种方法来添加此。VBA多个IF值

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 
+0

你想对每个方向使用相同的颜色或不同的颜色吗? –

回答

1

给这个一杆。根据需要更新颜色分配:

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 
+0

完美的作品!谢谢。 – Kaz

0

考虑:

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 

或:

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 
2

尝试使用Select Case而不是If语句。

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 
+0

从未使用过Select Case之前,我会看看它。谢谢! – Kaz

+0

Select Case经常被新程序员忽略,但它可以使代码更容易遵循和理解。请享用! –

+0

[Select ... Case Statement(Visual Basic)](https://msdn.microsoft.com/en-us/library/cy37t14y.aspx) –