2016-08-22 42 views
0

我試圖執行一堆IF語句時遇到了一個語句,是的,這聽起來很奇怪,但我無法將我的頭圍繞我需要做的事情。如果條件滿足,然後運行多個ifs

本質上,我想在單元格(i,j)中計數j = 1到6,我爲i = 1解除綁定(a)(不再有多行)。然後,如果它計數c> = 3,那麼它應該執行所有這些if語句以使匹配單元格具有索引顏色。當然,我的代碼非常粗糙且效率低下,所以請根據您的需要進行更改。我仍然在學習VBA,而且我喜歡在我的空閒時間做這件事,可能性是無限的!

我希望它是有道理的是我正在嘗試做什麼。

Sub Visrigtige()Dim b(48) As Boolean, x, a 
Dim c, i As Long, j As Long, n As Long, f As Long, g As Long, h As Long 


Columns("A:F").Interior.Color = xlNone 
Range("M2:R2").Interior.Color = xlNone 


    Cells(2, "m").Interior.ColorIndex = 34 
    Cells(2, "n").Interior.ColorIndex = 35 
    Cells(2, "o").Interior.ColorIndex = 36 
    Cells(2, "p").Interior.ColorIndex = 38 
    Cells(2, "q").Interior.ColorIndex = 39 
    Cells(2, "r").Interior.ColorIndex = 40 


x = Array(Range("M2"), Range("N2"), Range("O2"), Range("P2"), Range("Q2"),  Range("R2")) 'change this to suit, or use input box or other 


a = Range("A1").CurrentRegion.Resize(, 6).Rows 
For Each c In x: b(c) = True: Next c 
For i = 1 To UBound(a) 
c = 0 
For j = 1 To 6 
    If b(a(i, j)) Then c = c + 1 
    Next j 
     If c >= 3 Then 
      If Cells(i, j) = Cells(2, "m") Then Cells(i, j).Interior.ColorIndex = 34 
      If Cells(i, j) = Cells(2, "n") Then Cells(i, j).Interior.ColorIndex = 35 
      If Cells(i, j) = Cells(2, "o") Then Cells(i, j).Interior.ColorIndex = 36 
      If Cells(i, j) = Cells(2, "p") Then Cells(i, j).Interior.ColorIndex = 38 
      If Cells(i, j) = Cells(2, "q") Then Cells(i, j).Interior.ColorIndex = 39 
      If Cells(i, j) = Cells(2, "r") Then Cells(i,  j).Interior.ColorIndex = 40 
    End If 
Next i 
End Sub 

你們知道我的代碼的最後部分有什麼問題嗎?

特別是這部分:

If b(a(i, j)) Then c = c + 1  Next j 
     If c >= 3 Then 
      If Cells(i, j) = Cells(2, "m") Then Cells(i, j).Interior.ColorIndex = 34 
      If Cells(i, j) = Cells(2, "n") Then Cells(i, j).Interior.ColorIndex = 35 
      If Cells(i, j) = Cells(2, "o") Then Cells(i, j).Interior.ColorIndex = 36 
      If Cells(i, j) = Cells(2, "p") Then Cells(i, j).Interior.ColorIndex = 38 
      If Cells(i, j) = Cells(2, "q") Then Cells(i, j).Interior.ColorIndex = 39 
      If Cells(i, j) = Cells(2, "r") Then Cells(i, j).Interior.ColorIndex = 40 
    End If 
Next i 
+0

究竟是什麼問題?是否發生錯誤?如果是,在哪一行? –

+0

'b(a(i,j))'預計會產生一個布爾值(true和false)。就像現在一樣,沒有什麼能夠創建「TRUE/FALSE」值。我不知道你的目標是什麼。你可以在我們的電子表格中提供數據嗎? 'Range(「A1:A6」)'中的值是多少? – Spurious

+0

我認爲使用SELECT CASE作爲着色索引部分會更具可讀性。 –

回答

0

它的好,大家好,我理解了它自己。

Sub Visrigtige() 
Dim b(48) As Boolean, x, a 
Dim c, i As Long, j As Long 
Dim k As Long 

Columns("A:G").Interior.Color = xlNone 
Range("M2:R2").Interior.Color = xlNone 

    Cells(2, "m").Interior.ColorIndex = 34 
    Cells(2, "n").Interior.ColorIndex = 35 
    Cells(2, "o").Interior.ColorIndex = 36 
    Cells(2, "p").Interior.ColorIndex = 38 
    Cells(2, "q").Interior.ColorIndex = 39 
    Cells(2, "r").Interior.ColorIndex = 40 

x = Array(Range("M2"), Range("N2"), Range("O2"), Range("P2"), Range("Q2"), Range("R2")) 'change this to suit, or use input box or other 

a = Range("A1").CurrentRegion.Resize(, 6).Rows 
For Each c In x: b(c) = True: Next c 
For i = 1 To UBound(a) 
c = 0 
For j = 1 To 6 
    If b(a(i, j)) Then c = c + 1 
    Next j 
    For k = 1 To 6 
    If c >= 3 And Cells(i, k) = Cells(2, "m") Then Cells(i, k).Interior.ColorIndex = 34 
    If c >= 3 And Cells(i, k) = Cells(2, "n") Then Cells(i, k).Interior.ColorIndex = 35 
    If c >= 3 And Cells(i, k) = Cells(2, "o") Then Cells(i, k).Interior.ColorIndex = 36 
    If c >= 3 And Cells(i, k) = Cells(2, "p") Then Cells(i, k).Interior.ColorIndex = 38 
    If c >= 3 And Cells(i, k) = Cells(2, "q") Then Cells(i, k).Interior.ColorIndex = 39 
    If c >= 3 And Cells(i, k) = Cells(2, "r") Then Cells(i, k).Interior.ColorIndex = 40 
    Next k 
Next i 
End Sub 

不知道你是否可以看到我做了什麼,但我Dim k長,並做了一個新的循環我的代碼來檢查。

相關問題