我正在尝试在满足一个语句时执行一堆IF语句,是的,这听起来很奇怪,但我无法理解我需要做的事情。
基本上我想数" 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)
好吧,我自己想出来了。
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
不确定你是否可以看到我做了什么,但是我愣了一下,然后我的代码进行了新的循环检查。