计算单元格是否为空白

时间:2014-10-06 15:05:36

标签: excel vba excel-vba

大家好我有2个公式我试图跑,但只有当小区不空白时才努力让COUNTIF计算。

Sheets("Home").Select
If Range("A2:A14").Count = "13" Then

MsgBox "Current Load Full Please Complete & Export", vbCritical

Exit Sub

End If

第二代码

Sheets("Home").Select
If Range("A2:A14").Count < "13" Then

MsgBox "Shipment is short do you want to continue?", vbCritical vbYesNo

Exit Sub

End If

在第二个代码上,如果vbYes然后运行代码,如果vbNo然后退出sub。

2 个答案:

答案 0 :(得分:1)

如果您正在尝试根据条件执行某些操作“条件A2:A14中的所有单元格都已填充” - 那么此代码可能就是答案。

Sub check_count()

Sheets("Home").Select

Dim myRange As Range
Set myRange = Worksheets("Home").Range("A2:A14")

'using excel's built in function CountA to check count of non-blank cells
'if the count is 13 - then msgbox
If Application.WorksheetFunction.CountA(myRange) = 13 Then
    MsgBox "Current Load Full Please Complete & Export", vbCritical
    Exit Sub
'if the count is less then 13 - then do following
Else:
    msg1 = MsgBox("Shipment is short do you want to continue?", vbYesNo)
    If msg1 = vbYes Then
        MsgBox "Enter missing products in A2:A14" 'you can run some code here as well
    Else: Exit Sub
    End If
End If

End Sub

希望这能回答你的问题。

答案 1 :(得分:0)

为了计算给定范围内的所有非空白单元格,您可以使用:

If ActiveSheet.Range("A2:A14").SpecialCells(xlCellTypeConstants).Count < 13 Then