Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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 对已用行中的空单元格进行计数_Excel_Vba_Count - Fatal编程技术网

Excel 对已用行中的空单元格进行计数

Excel 对已用行中的空单元格进行计数,excel,vba,count,Excel,Vba,Count,我有下面的VBA代码,我想要的是计算使用的行数,然后在B列中找到空单元格的数量,它返回一个语法错误 Sub Logic() 'Count Number of used Rows LastRow = Worksheets("TASK").UsedRange.Rows.count Range("O3") = LastRow 'Count Number of Empty cells within the used Row Dim EmptyCell As

我有下面的VBA代码,我想要的是计算使用的行数,然后在B列中找到空单元格的数量,它返回一个语法错误

Sub Logic()
 'Count Number of used Rows
LastRow = Worksheets("TASK").UsedRange.Rows.count 
Range("O3") = LastRow


'Count Number of Empty cells within the used Row
Dim EmptyCell As Integer
EmptyCell = Application.WorksheetFunction.CountIf(Range("B1":"B" & LastRow), "")
Range("O4") = EmptyCell 

End Sub

感谢您的帮助

下面是一个案例。应该记住,.UsedRange在填充工作表的单元格时会动态变化,并且会产生不可预测的结果-一切都取决于任务的设置和数据的配置

Option Explicit

Sub Logic()
    With Worksheets("TASK")
        'Count Number of Empty cells within the used Rows
        .Range("O4") = WorksheetFunction.CountBlank( _
            Intersect(.UsedRange, .Columns("B")))
    End With
End Sub

范围(“B1”:“B”和LastRow)
应该是
范围(“B1”、“B”和LastRow)
范围(“B1:B”和LastRow)
。我建议为每个
范围指定该范围在哪个工作表中。像
工作表(“任务”).Range…
更易于使用
工作表函数.COUNTBLANK()
不要使用
UsedRange
否则您可能会得到不正确的空白计数。找到最后一行,如图所示,然后在相关范围内查找空白单元格,使用<代码>工作表函数。然后,针对特定列中的最后一行可以减少间隙数-如果表中列的末尾有间隙。