2016-08-22 3 views
0

Я пытаюсь выполнить кучу IF-операторов, когда выполняется инструкция ONE, да, это звучит странно, но я не могу окунуться в то, что мне нужно сделать.Если условие выполнено, запустите несколько ifs

По сути, я хочу считать «c» в ячейках (i, j) для j = 1-6 и i для 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». Я не знаю, какова ваша цель. Можете ли вы предоставить нам данные в вашей таблице. Каковы значения в «Диапазон (« 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 до тех пор, и сделал новую петлю моего код для проверки.

Смежные вопросы