Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将CountIf公式以VBA代码语法添加到单元格中_Excel_Vba - Fatal编程技术网

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

Excel 将CountIf公式以VBA代码语法添加到单元格中,excel,vba,Excel,Vba,我想知道是否有人能帮我理解这个语法?我正在尝试将一个CountIf公式插入我的摘要工作表(这部分代码中的活动工作表)中的单元格,并尝试引用工作簿其他工作表中的单元格。我这样做的原因是,我希望在汇总表中立即更新工作表(汇总表除外)中发生的任何更改 到目前为止,我对这一部分的理解是: If Z=13 Then 'Z denotes the Sheet, in this case Summary Sheet looped = ActiveSheet.Range("A1048576").End(x

我想知道是否有人能帮我理解这个语法?我正在尝试将一个
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列,看看“未测试”是否在该单元格中。如果是,数一数,依此类推


这可能吗?

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

或者,我在下面的不同子部分中有两个示例,用于总结每张工作表和VBA中的工作簿。我想这就是你要找的。您可以将值打印到所需的单元格中,而不是直接打印到即时窗口中

如果您注意到我在代码中使用了
.Rows.Count
,然后在xlUp之前获得最后一行。这将保持该代码与其他版本的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
选项显式
未测试总数的子计数()
'在汇总表之前汇总工作簿的数据并打印到即时窗口
将工作作为工作表
最后一排一样长
暗淡的碳纳米管
我想我会坚持多久
施工汇总表=汇总表的13'索引
用于此工作簿中的每个工作。工作表
有工作
'对于汇总表之前的所有工作表
”(如果它们是按顺序创建的,则索引应在
'顺序,否则应使用名称)
如果.索引<汇总表,则
'获取列A当前工作表的最后一行
lastRow=.Cells(.Rows.Count,1).End(xlUp).Row
'循环浏览当前工作表中A列的所有行
对于i=1到最后一行
'未测试列时增加cnt
“为了进行比较,强制使用共同资本化很好
如果UCase(.Cells(i,1))=“UNTESTED”,则cnt=cnt+1
接下来我
如果结束
以
接下来的工作
Debug.Print“未测试的数量:”&cnt
端接头
每个表中未测试的子计数()
'在汇总表之前汇总每张表的数据并打印到即时窗口
将工作作为工作表
最后一排一样长
暗淡的碳纳米管
我想我会坚持多久
施工汇总表=汇总表的13'索引
用于此工作簿中的每个工作。工作表
有工作
'每个工作表重置为0
cnt=0
'对于汇总表之前的所有工作表
”(如果它们是按顺序创建的,则索引应在
'顺序,否则应使用名称)
如果.索引<汇总表,则
'获取列A当前工作表的最后一行
lastRow=.Cells(.Rows.Count,1).End(xlUp).Row
'循环浏览当前工作表中A列的所有行
对于i=1到最后一行
'未测试列时增加cnt
“为了进行比较,强制使用共同资本化很好
如果UCase(.Cells(i,1))=“UNTESTED”,则cnt=cnt+1
接下来我
如果结束
“每张纸上都有摘要
Debug.Print“&.Name&“:”和cnt的“未测试数量”
以
接下来的工作
端接头