2016-05-10 17 views
1

私は列をループして、各行が別の列に現れる回数を数えます。これは一度動作し、正しいカウントを表示します。しかしそれ以降は、すべての行に対してゼロが表示されます。私はコードをステップし、カウンタの値は更新されません。もし誰かが見ていただければ幸いです。ネストされたForループでカウンタがインクリメントしない - Excel VBA

Sub count_tic_types() 
     Dim ticCountGraph As Worksheet 
     Dim CWData   As Worksheet 
     '' Row count for ticCountGraph. 
     Dim i    As Integer 
     '' Row count for CWData 
     Dim j    As Integer 
     Dim ticCount  As Integer 
     Dim ticAlert  As Integer 

     Set ticCountGraph = Worksheets("ticCountGraph") 
     Set CWData = Worksheets("CWData") 


     For i = 1 To 9 

      '' Start the counters fresh for each new row. Ensures that 
      '' there's no double counting. 
      ticCount = 0 
      ticAlert = 0 

      '' Loop through entire target column to count the number of 
      '' matches there. 
      For j = 2 To CWData.Range("A2").End(xlDown).Row 

       '' Want to count values that have been Closed. 
       If Range("I" & j).Value = "Closed" Then 

        '' Increment if there are mathces. 
        If CWData.Range("J" & j).Value = ticCountGraph.Range("A" & i).Value Then 
          ticCount = ticCount + 1 

        '' Count blank values separately. 
        ElseIf IsEmpty(CWData.Range("J" & j)) Then 
          ticAlert = ticAlert + 1 

        End If 
       End If 
      Next 

      If ticCountGraph.Range("A" & i).Value = "Alerts" Then 
       ticCountGraph.Range("B" & i).Value = ticAlert 
      Else 
       ticCountGraph.Range("B" & i).Value = ticCount 
      End If 
     Next 
    End Sub 

iがループスルーする列。 enter image description here

カラムj意志ループを介して上記画像内の各列に一致発生 の数を数えます。

enter image description here

+1

範囲( "A"&1)この1も増分またはjのようなvarにする必要がありますか? –

+0

私はあなたのコードとデータの目標をテストするための情報をいくつか与えるべきだと思います。いくつかのブラインドショット:1)Range( "I"&j) 'にシート参照を追加する。 2) 'ticCount'カウンタは、J列の値がticCountGraph.Range(" A "&i)'に等しいときにインクリメントされますが、後者が "Alerts"の場合は 'ticCount'カウンタを書き留めません。... – user3598756

答えて

2

私は今、それを得ました。問題は、指定されていないワークシートのRange("I"&j).Valueである可能性があります。私はCWDataで指定した後に動作します。

レッスン:異なるワークシートの範囲で作業する場合は、すべての関数に正しいワークシート名を付けるようにしてください。

For i = 2 To 10 

    '' Start the counters fresh for each new row. Ensures that 
    '' there's no double counting. 
    ticCount = 0 
    ticAlert = 0 

    For j = 2 To CWData.Range("A2").End(xlDown).Row 

     '' THIS IS THE PART THAT REQUIRED WORKSHEET NAME. 
     If CWData.Range("I" & j).Value <> ">Cancelled" Then 
      If CWData.Range("J" & j).Value = ticCountGraph.Range("A" & i).Value Then 
        ticCount = ticCount + 1 
      ElseIf IsEmpty(CWData.Range("J" & j)) Then 
        ticAlert = ticAlert + 1 

      End If 
     End If 
    Next j 
    If ticCountGraph.Range("A" & i).Value = "Alerts" Then 
     ticCountGraph.Range("B" & i).Value = ticAlert 
    Else 
     ticCountGraph.Range("B" & i).Value = ticCount 
    End If 

Next i 
+0

良い仕事、あなたがそれを見つけてうれしい。 – Andreas

1
Sub count_tic_types() 
    Dim ticCountGraph As Worksheet 
    Dim CWData   As Worksheet 
    '' Row count for ticCountGraph. 
    Dim i    As Integer 
    '' Row count for CWData 
    Dim j    As Integer 
    Dim ticCount  As Integer 
    Dim ticAlert  As Integer 

    Set ticCountGraph = Worksheets("ticCountGraph") 
    Set CWData = Worksheets("CWData") 

    For i = 1 To 9 

     '' Start the counters fresh for each new row. Ensures that 
     '' there's no double counting. 
     ticCount = 0 
     ticAlert = 0 

     For j = 2 To CWData.Range("A2").End(xlDown).Row 

      If Range("I" & j).Value = "Closed" Then 

       If CWData.Range("J" & j).Value = ticCountGraph.Range("A" & i).Value Then 
         ticCount = ticCount + 1 

       ElseIf IsEmpty(CWData.Range("J" & j)) Then 
         ticAlert = ticAlert + 1 

       End If 
      End If 
     Next j ' forgot j 

     If ticCountGraph.Range("A" & i).Value = "Alerts" Then 
      ticCountGraph.Range("B" & i).Value = ticAlert 
     Else 
      ticCountGraph.Range("B" & i).Value = ticCount 
     End If 
    Next i ' forgot i 
End Sub 
+0

@Dombey ticCountとticAlertは各ループで0にリセットされるはずですか? – Andreas

+0

@Dombeyコードが異なる行をどのようにチェックするのかわかりません。私は "j"ループのしくみを見ることができますが、 "i"ループは何度も同じことを繰り返します。または??データをテストしないで理解するのは難しいです。 – Andreas

関連する問題