我正在尝试在Excel宏中使用Application.CountIf()
并且它没有返回计数。它将数字返回为0。
我发现这令人困惑,因为我在另一个宏中多次使用Application.CountIf()
。
来自其他宏的工作代码:
Sub newer_COA()
Sheets("BATCH NUMBERS").Select
'Count total of column CO
count = Application.CountIf(Columns(93), "1")
End Sub
新宏代码 - sum_litres()
Sub sum_litres()
Workbooks("Small Fill.xlsm").Activate
Sheets("Small Fill").Select
'Count total Machine one entries in column F
Dim Machine_one_count As Integer
Machine_one_count = Application.CountIf(Columns(6), "1")
Workbooks("Small Fill Analysis.xlsm").Activate
Sheets("Sheet1").Select
Msg = Machine_one_count & " Number of entries from Machine one"
MsgBox Prompt:=Msg
End Sub
新宏的输出 - sum_litres()
0 Number of entries from Machine one
我在名为sum_litres()
的单独工作表中创建了这个新的宏Small Fill Analysis
,该工作表会让工作表Small Fill.xlsm
查看数据。在sum_litres()
开始时,它使用下面的函数检查工作表Small Fill.xlsm
是否已打开,如果工作表尚未打开则成功打开它。由于此代码工作正常,我没有在上面的问题中包含它。
'Calls function IsWorkBookOpen() to check if the required spreadsheet is open
Ret = IsWorkBookOpen("Small Fill.xlsm")
If Ret = True Then
Workbooks("Small Fill.xlsm").Activate
Sheets("Small Fill").Select
Else
'Open required spreadsheet
Workbooks.Open FileName:="Small Fill.xlsm", ReadOnly:=True
Sheets("Small Fill").Select
End If
Function IsWorkBookOpen(ByVal FileName As String) As Boolean
Dim TargetWorkbook As Workbook
Dim IteratorWorkbook As Workbook
For Each IteratorWorkbook In Application.Workbooks
If IteratorWorkbook.FullName = FileName Then
Set TargetWorkbook = IteratorWorkbook
End If
Next
If Not TargetWorkbook Is Nothing Then
If TargetWorkbook.ReadOnly Then
IsWorkBookOpen = True
Exit Function
End If
End If
End Function
非常感谢任何建议!
答案 0 :(得分:0)
我怀疑问题是宏的位置,但解决方案只是不选择东西,而是直接引用范围:
Sub sum_litres()
'Count total Machine one entries in column F
Dim Machine_one_count As Integer
Machine_one_count = Application.CountIf(Workbooks("Small Fill.xlsm").Sheets("Small Fill").Columns(6), "1")
Msg = Machine_one_count & " Number of entries from Machine one"
MsgBox Prompt:=Msg
End Sub