2016-08-22 91 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长,并做了一个新的循环我的代码来检查。