2017-01-03 9 views
0

誰かがこの構文で私を助けることができるのだろうか?私はCountIf数式をセルの集計シート(コードのこの部分のアクティブシート)に挿入しようとしていますが、ブックの他のワークシートのセルを参照しようとしています。私がこれをやっているのは、要約シートですぐに更新されるように、要約シート以外のワークシートで何らかの変更が起こりたいからです。私はこの部分のために、これまで持って何CountIf数式をVBAコードのセルに追加する

は次のとおりです。

If Z=13 Then 'Z denotes the Sheet, in this case Summary Sheet 
    looped = ActiveSheet.Range("A1048576").End(xlUp).Row 
    For i=1 to looped 'going through the rows 
    Cells(i, 1) = "=Countifs(Sheets1!A"& i & "Sheets1!A",Sheets2!A," & """ & ""UNTESTED"" & "")"" 
    Next 
End If 

私は問題を抱えています私は何をするには、このラインのために何をしたいです

Cells(i, 1) = "=Countifs(Sheets1!A" & i & "Sheets1!A",Sheets2!A," & """ & ""UNTESTED"" & "")"" 

の構文は行くことです13番目のワークシートの前の各ワークシートと、各行について、そのワークシートの列iを調べ、そのセルに「UNTESTED」が含まれているかどうかを確認します。そうであれば、カウントしてください。

これは可能ですか?

答えて

0

countifsである必要がありますか?さもなければ、ちょうど指針としてexcelを使用してcountifsを作成し、次にそれを別々に記入しないでください。同様に、Excelで作業countifを取得し、VBAコードに入れます。上で使用しているcountifの構文は正しくありません。また、各セルにcountifステートメントを上書きするようにも見えますが、これはあなたが望むものではないと思います。

また、ここでは、1つのシートにつき要約するVBAのワークブックと、VBAのワークブックをまとめた2つの例を示します。私はこれらのうちの1つがあなたが探しているものだと思います。直前のウィンドウではなく、必要なセルに値を印刷することができます。

コードに記載されている場合は、xlUpの前に.Rows.Countを使用して最後の行を取得します。これにより、他のバージョンのExcelとのコードの互換性が維持されます。ほとんどの場合、すべてのバージョンに1048576が含まれているわけではありません。

Option Explicit 
Sub count_of_untested_total() 
'Summarizes data for the workbook before summary sheet and prints to immediate window 
Dim wks As Worksheet 
Dim lastRow As Long 
Dim cnt As Long 
Dim i As Long 

Const SUMMARY_SHEET = 13 'Index of the summary sheet 

For Each wks In ThisWorkbook.Worksheets 
    With wks 
     'For all sheets before the summary sheet 
     '(if they were created in order the index should be in 
     'order, otherwise you should use names) 
     If .Index < SUMMARY_SHEET Then 
      'Get last row of current sheet for column A 
      lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 

      'Loop through all rows for column A in current sheet 
      For i = 1 To lastRow 
       'increment cnt when column has untested 
       'good to enforce common capitalization for comparisons 
       If UCase(.Cells(i, 1)) = "UNTESTED" Then cnt = cnt + 1 
      Next i 
     End If 
    End With 
Next wks 

Debug.Print "Number of Untested: " & cnt 

End Sub 

Sub count_of_untested_per_sheet() 
'Summarizes data per sheet before summary sheet and prints to immediate window 
Dim wks As Worksheet 
Dim lastRow As Long 
Dim cnt As Long 
Dim i As Long 

Const SUMMARY_SHEET = 13 'Index of the summary sheet 

For Each wks In ThisWorkbook.Worksheets 
    With wks 
     'reset to 0 for each sheet 
     cnt = 0 
     'For all sheets before the summary sheet 
     '(if they were created in order the index should be in 
     'order, otherwise you should use names) 
     If .Index < SUMMARY_SHEET Then 
      'Get last row of current sheet for column A 
      lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 

      'Loop through all rows for column A in current sheet 
      For i = 1 To lastRow 
       'increment cnt when column has untested 
       'good to enforce common capitalization for comparisons 
       If UCase(.Cells(i, 1)) = "UNTESTED" Then cnt = cnt + 1 
      Next i 
     End If 
     'Summary on each sheet 
     Debug.Print "Number of Untested for " & .Name & ": " & cnt 
    End With 
Next wks 

End Sub 
関連する問題