2017-02-27 181 views
-3

我想寫一個宏代碼,將做如下的單元格區域VBA條件格式細胞值大於或小於

  1. 條件格式將僅在細胞有申請已輸入值
  2. 如果單元格中的值小於$ I $ 6和/或大於$ M $ 6,則突出顯示紅色,如果不是,請不要突出顯示(或不應用)。

這是作爲一個規格檢查輸入的數據,以確保數字在規格範圍內。謝謝!

我試了一下:

Sub SpecCheck() 
    Dim iRow As Range 
    Set iRow = Range("f16:l34") 

    For Each cell In iRow 
     If cell.Value <> "" And cell.Value > "I6" And cell.Value < "M6" Then 
      cell.Interior.Color = RGB(255, 0, 0) 
     End If 
    Next 
End Sub 

新的代碼我試過了,沒有工作。也不確定是否有問題,但代碼是用工作表的「常規」代碼編寫的。

Sub SpecCheck() 

Dim iRow As Range, cell As Range 
Dim lowSpec As Range 
Dim highSpec As Range 
Set iRow = Range("f16:l34") 
Set lowSpec = Range("r6") 
Set highSpec = Range("s6") 

For Each cell In iRow 
If cell.Value <> "" And cell.Value > highSpec And cell.Value < lowSpec Then 
    cell.Interior.Color = RGB(255, 0, 0) 
End If 
Next 

End Sub 
+0

**'我想編寫一個宏code' ** ......好了,這是怎麼回事...? SO不是**'「爲我的網站編寫代碼」**。告訴我們你到目前爲止試過的東西以及你卡在哪裏。 – ManishChristian

+0

子SpecCheck() 昏暗iRow作爲範圍設定 = iRow範圍( 「F16:1-34」) 針對每個小區在iRow 如果cell.Value <> 「」 而cell.Value> 「I6」 和細胞。值<「M6」Then cell.Interior.Color = RGB(255,0,0) End If Next End Sub –

回答

1

你需要指定那些範圍,而不是字符串:

Sub SpecCheck() 
Dim iRow As Range, cell as Range ' I also added the cell as Range 
Set iRow = Range("f16:l34") 

For Each cell In iRow 
    If cell.Value <> "" And cell.Value > Range("$I$6").Value And cell.Value < Range("$M$6").Value Then 
     cell.Interior.Color = RGB(255, 0, 0) 
    End If 
Next 
End Sub 
+0

合併單元格是否會影響代碼?我的兩個範圍是在一個合併單元格... –

+0

@ Sid.T。 - 是的,它會。你能發佈一個快速截圖嗎?您必須取消合併,並確保兩個單元格都有數據。通常最好避免合併的單元格,並且只使用格式來使它們看起來合併,IMO。你能澄清嗎? '$ I $ 6'就像'Value is 123',你只需要檢查'123'? – BruceWayne

+0

只是爲了檢查它是否是合併的單元格,我將單元格範圍更改爲r6和s6。我想檢查的值是低於299,如果高於311.Private Sub SpecCheck() Dim iRow As Range,cell As Range'我還添加了單元格作爲範圍 Dim lowSpec As Range Dim highSpec As Range 集iRow =範圍( 「F16:1-34」) 集lowSpec =範圍( 「R6」) 集highSpec =範圍( 「S6」) 針對每個小區在iRow 如果cell.Value <> 「」 和細胞.value的> highSpec而cell.Value

相關問題