在VBA代码语法中将CountIf公式添加到单元格中

时间:2017-01-03 19:33:36

标签: excel-vba vba excel

我想知道是否有人可以帮助我使用这种语法?我试图在我的摘要表(这部分代码中的活动表)中的单元格中注入一个CountIf公式,我正在尝试引用工作簿的其他工作表中的单元格。我这样做的原因是因为我希望在汇总表以外的工作表中进行任何更改,以便在汇总表中立即更新。

到目前为止,这部分内容是:

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个工作表之前的每个工作表,并且对于每个行,我,对于该工作表,查看列A并查看“UNTESTED”是否在该单元格中。如果是,则计数,等等。

这可能吗?

1 个答案:

答案 0 :(得分:0)

它必须是countifs吗?否则,为什么不使用excel作为指导创建countifs,然后单独填写?在中,获取excel中的工作countif,然后将其放入VBA代码中。您在上面使用的countifs的语法不正确。看起来你也会用countif语句覆盖每个单元格,我认为这也不是你想要的。

或者,我在下面的不同子代中有两个示例,用于总结每张表和VBA中的工作簿。我认为其中一个就是你要找的东西。您可以将值打印到所需的单元格中,而不是立即窗口。

如果您在代码中注意到我在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