2011-06-10 75 views
1

我已經寫了下面的代碼,它基本上是相應的顏色一些盒子。每當我運行這段代碼時,它都會運行第一種情況,即使在需要選擇其他一些情況時也是如此。這裏是代碼。使用if條件多條件

Sub Macro_quaterly() 
If Sheet2.Range("B6").Value = 1 Or 2 Or 3 Then 
    Range("D7").Select 
    With Selection.Interior 
     '.Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
     Sheet2.Cells(6, 11) = "rrrrrrr" 
    End With 
ElseIf Sheet2.Range("B6").Value = 4 Or 5 Or 6 Or 7 Then 
    Range("D7:E7").Select 
    With Selection.Interior 
     '.Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
     Sheet2.Cells(6, 12) = "rddddddr" 
    End With 
ElseIf Sheet2.Cells(6, 2) = 8 Or 9 Or 10 Or 11 Then 
Range("D7:F7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 12 Or 13 Or 14 Or 15 Then 
Range("D7:G7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 16 Or 17 Or 18 Or 19 Then 
Range("D7:H7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 20 Or 21 Or 22 Or 23 Then 
Range("D7:I7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 24 Or 25 Or 26 Or 27 Then 
Range("D7:J7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 28 Or 29 Or 30 Or 31 Then 
Range("D7:K7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 32 Or 33 Or 34 Or 35 Then 
Range("D7:L7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 36 Or 37 Or 38 Or 39 Then 
Range("D7:M7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 40 Or 41 Or 42 Or 43 Then 
Range("D7:N7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 44 Or 45 Or 46 Or 47 Then 
Range("D7:O7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 48 Or 49 Or 50 Or 51 Then 
Range("D7:P7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 52 Or 53 Or 54 Or 55 Then 
Range("D7:Q7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 56 Or 57 Or 58 Or 59 Then 
Range("D7:R7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
ElseIf Sheet2.Cells(6, 2) = 60 Then 
Range("D7:S7").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
End If 

End Sub 

您的幫助,將不勝感激。

+2

多德。你可以在[代碼]標籤中發佈該代碼,並且可以使用諸如換行符或兩個代碼之類的代碼? – 2011-06-10 12:12:21

+0

哇!我希望能讀到... – diagonalbatman 2011-06-10 12:13:47

+1

現在全部清理。 – jonsca 2011-06-10 12:18:47

回答

5
If Sheet2.Range("B6").Value = 1 Or 2 Or 3 Then 

此行是不是做了什麼你覺得它在做什麼。你需要輸入If Sheet2.Range("B6").Value = 1 Or Sheet2.Range("B6").Value = 2 Or Sheet2.Range("B6").Value = 3 Or Sheet2.Range("B6").Value = 4 Then(或者用一箇中間變量代替Sheet2.Range("B6").Value

+0

我一直想看看是否支持if(foo ==(bar或baz或biz))......因爲它似乎我一直在打字,最初...... – 2011-06-10 15:32:27

1

好的, 所以這裏的問題是「If語句」。

定義正確的方法「或」是如此

If Sheet2.Range("B6").Value = 1 Or Sheet2.Range("B6").Value = 2 Or Sheet2.Range("B6").Value = 3 Then 
9

另一種方法是使用Select..Case語句。我認爲這是一個很大的可讀性這樣的事情:

Select Case Sheet2.Range("B6").Value 
Case 1, 2, 3 
    Range("D7").Select 
    With Selection.Interior 
     '.Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
     Sheet2.Cells(6, 11) = "rrrrrrr" 
    End With 
Case 4, 5, 6, 7 
    Range("D7:E7").Select 
    With Selection.Interior 
     '.Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
     Sheet2.Cells(6, 12) = "rddddddr" 
    End With 
Case .... 
    .... 
Case Else 
    .... 
End Select 
+2

我可能會爭辯說,是做這件事的最好方式 - 並閱讀OP使用「Case」這個詞組的問題 - 也許這是最初的意圖? – diagonalbatman 2011-06-10 13:44:18

4

回答你的問題在於這樣一個事實:在您的Or條件的數字都隱含強制轉換爲布爾值,而當這種情況發生,除0之外的所有內容都被強制爲True。要說服你自己,請嘗試Debug.Print CBool(13)Debug.Print CBool(0)

我有點厭煩,沒有任何一位發佈了以前的答案的人解釋過這一點,因此這篇文章可能被認爲是重複的!

而不是

If Sheet2.Range("B6").Value = 1 Or 2 Or 3 Then 

使用

If Sheet2.Range("B6").Value = 1 Or _ 
    Sheet2.Range("B6").Value = 2 Or _ 
    Sheet2.Range("B6").Value = 3 Then 

等,或者甚至更好,如@ mwolfe02提出了Select Case結構。

1

除了這裏發佈的其他答案提到的錯誤之外,重要的是要注意用於測試您的病症的構造是IfElseIf。通過使用它來測試您的條件,您將始終執行解析爲True的第一個ElseIf條件,並跳過可能定義的任何後續條件。

這意味着根據您打算格式化的適當條件,最終可能會出現邏輯問題。

因此,我建議使用上述響應中@ mwolfe02提供的Case語句結構以及相同Case語句中的所有類似格式條件。這將防止各種格式化情況發生的基礎上,特定的數據順序已滿足什麼條件。

希望有所幫助。

+0

謝謝大家.. :-) – mehak 2011-06-13 05:27:24

+0

嘿..theres在同一代碼中面臨的另一個問題..我猜它應該是簡單的,但我真的是新的vba..could任何人都可以幫我請例如..我使用以下vba代碼來簡單地將現有單元格的值分配給另一個單元格中的另一個單元格......但它給出了運行時錯誤424..Kindly help..the代碼如下所示 Sub Selection_cell_for_payoff_quaterly() 如果Sheet2.Cells( 6,2)。價值= 1然後 Sheet2.Cells(7,2)。價值= Sheet6.Range( 「D7」)值 結束如果 結束子 – mehak 2011-06-13 05:47:21

+1

@mehak:請不要問新問題在評論中。只需發佈一個新問題。 – 2011-06-14 19:31:37

0

大量的冗餘代碼,你可以擺脫,並提到幾次去select case。

嘗試:

Sub Macro_quaterly() 
    Dim rCell As Range 

    Select Case Sheet2.Range("B6").Value 
    Case 1, 2, 3 
     Set rCell = Range("D7") 
     Sheet2.Cells(6, 11) = "rrrrrrr" 
    Case 4, 5, 6, 7 
     Set rCell = Range("D7:E7") 
     Sheet2.Cells(6, 12) = "rddddddr" 
    Case 8, 9, 10, 11 
     Set rCell = Range("D7:F7") 

    Case Else 

    End Select 

    With rCell.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 

    Set rCell = Nothing 

End Sub