2016-08-22 7 views
0

1つのステートメントが満たされたときに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値を生成するものは何もありません。私はあなたの目標が何であるか分かりません。スプレッドシートのデータを教えてください。 'Range(" A1:A6 ")の値は何ですか? – Spurious

+0

私は、色分けインデックス部分にSELECT CASEを使用する方が読みやすいと思います。 –

答えて

0

その大丈夫みんな、私は自分自身をそれを考え出しました。あなたは私が何をしたか見ることができますが、私は限りkおよび新しいループにチェックするために私のコードを作っ薄暗い場合

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 

わかりません。

関連する問題