Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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
Vba 如何计算excel中非零的数量?_Vba_Excel - Fatal编程技术网

Vba 如何计算excel中非零的数量?

Vba 如何计算excel中非零的数量?,vba,excel,Vba,Excel,我试图制作一个宏,它将遍历整个工作簿,并计算员工的工作天数。工作表上的工作是以天为单位的,所以我们不必去寻找那些不是零的日子。我尝试使用了COUNTIF(A11:A12,“>0”),得到了错误Expected:list separator or)。我正在为每个循环使用一个,以完成工作表。我想把所有的信息放在工作簿末尾的一张新的表格上,写上员工的姓名和工作天数。我对VisualBasic非常陌生,但对c#非常熟悉 我现在已经走了这么远 Option Explicit Sub WorksheetL

我试图制作一个宏,它将遍历整个工作簿,并计算员工的工作天数。工作表上的工作是以天为单位的,所以我们不必去寻找那些不是零的日子。我尝试使用了
COUNTIF(A11:A12,“>0”)
,得到了错误
Expected:list separator or)
。我正在为每个循环使用一个
,以完成工作表。我想把所有的信息放在工作簿末尾的一张新的表格上,写上员工的姓名和工作天数。我对VisualBasic非常陌生,但对c#非常熟悉

我现在已经走了这么远

Option Explicit

Sub WorksheetLoop2()
    ' Declare Current as a worksheet object variable.
    Dim Current As Worksheet
    Dim LastColumn As Integer

    If WorksheetFunction.CountA(Cells) > 0 Then
    ' Search for any entry, by searching backwards by Columns.
        LastColumn = Cells.Find(What:="*", After:=[A1], _
        SearchOrder:=xlByColumns, _
        SearchDirection:=xlPrevious).Column
    End If

    ' Loop through all of the worksheets in the active workbook.
    For Each Current In Worksheets
        Current.Range("A27") = Application.WorksheetFunction.CountIf(Current.Range(Cells(11, LastColumn), Cells(16, LastColumn)), ">0")
        Current.Range("A28") = Application.WorksheetFunction.CountIf(Current.Range("Al17:Al22"), ">0")
    Next
End Sub

当我运行此操作时,我收到一个错误,提示
对象的方法范围''u工作表'失败
。我也无法找到一种方法来获取汇总表上的所有信息。

VBA解决方案,根据您上面的评论

好的VBA编程实践要求始终在代码中使用Option Explicit,这样您就可以知道何时没有正确声明变量,或者有时代码不好!在这种情况下,您可能会发现,仅仅写A27并不意味着您将值返回到单元格A27,而只是设置变量A27的值。或者你可能不太清楚,但你会很快发现你的问题出在哪里

此代码将为您修复它:

Option Explicit

Sub WorksheetLoop2()

'Declare Current as a worksheet object variable.
Dim Current As Worksheet

' Loop through all of the worksheets in the active workbook.
For Each Current In Worksheets
    Current.Range("A27") = Application.WorksheetFunction.CountIf(Current.Range("A11:A12"), ">0")
Next

End Sub
如果有帮助,非VBA解决方案:

假设您有一张汇总表,每个员工都在一张单独的表中,a列中有天数,B列中有工时,请在汇总表B1的公式栏中输入公式,并在a列中列出姓名列表


如果需要VBA,请将
Application.WorksheetFunction.
放在CountIf前面。如果您不需要VBA,可以仅使用公式推导出解决方案。按照Scott的建议,向前迈进一步。。使用此
Application.WorksheetFunction.CountIf(范围(“A11:A12”),“>0”)
我正在使用它,它不会抛出任何错误,但不会将数字计数放在27中,就像它应该“Sub WorksheetLoop2()”将当前值声明为工作表对象变量一样。在活动工作簿中的所有工作表中循环显示当前作为工作表。对于工作表A27=Application.WorksheetFunction.CountIf(范围(“A11:A12”),“>0”)下一个结束子项中的每个当前工作表,请参见下面对我的答案的编辑,基于此,您上次发表的评论。如果我在每张工作表的单元格中都有该名称,请参见。我如何将姓名和工作日写在汇总表上。谢谢所有的帮助哦,基本公式是
=mySheet1!A1
其中
mySheet
是工作表名称,
A1
是单元格引用。您也可以使用间接,就像我上面所做的那样,在单元格A1中键入mySheet,在B1中键入
=indirect(A1&“!A1”)
,以获取名称。