VBA检查给定范围内是否有任何单元格不为空

时间:2016-11-15 10:40:32

标签: excel vba excel-vba

我有以下代码,在保存工作簿之前,当特定工作表为空时会抛出提示。

代码的目的:要检查主板中的下拉值是否为“是”,如果为“是”,请检查特定工作表上的给定范围是否为空。如果“是”,则抛出提示并将主页上的下拉值更改为“否”。

关注:代码中的For loop将检查在给定范围内是否有任何单元格为空,而是我想要一个代码来检查给定范围内的任何一个单元格中是否有条目。假设给定范围是E10:G19,如果我们在E10中有一个条目,它应该来自代码并且不应该抛出提示并且只有在给定范围内的所有单元格都为空时才应该抛出。

问题:什么应该取代可以达到我目的的For loop

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet

Dim Rvalue As Range
Dim cell As Range
Set Rvalue = Sheets("Uni-corp").Range("E10:G19")

If Worksheets("Main").Range("E29").Value = "YES" Then
For Each cell In Rvalue
        If IsEmpty(cell) Then
            bOk = True
            Exit For
        Else: bOk = False
        End If
Next

If bOk Then
    If MsgBox("Sheet is blank", vbOKCancel + vbInformation) = vbOK Then
    Worksheets("Main").Range("E29").Value = "NO"
    Cancel = True
    End If
End If
End If
End Sub

4 个答案:

答案 0 :(得分:2)

你走了:

Option Explicit

Public Function b_is_range_empty(my_rng As Range)

    If Application.WorksheetFunction.CountA(my_rng) > 0 Then
        b_is_range_empty = False
    Else
        b_is_range_empty = True
    End If

End Function

Public Sub TestMe()

    Debug.Print b_is_range_empty(Selection)

End Sub

这个想法是在Excel中使用内置公式 - CountA。它针对更快的搜索进行了优化。在测试中,它适用于选择区域。

编辑: 取而代之的是:

For Each cell In Rvalue
        If IsEmpty(cell) Then
            bOk = True
            Exit For
        Else: bOk = False
        End If
Next

简单地写一下: bOK = b_is_range_empty(Rvalue)

答案 1 :(得分:1)

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet

Dim Rvalue As Range
Dim cell As Range
Set Rvalue = Sheets("Uni-corp").Range("E10:G19")

If Worksheets("Main").Range("E29").Value = "YES" Then
For Each cell In Rvalue
        If IsEmpty(cell)<>true Then
            bOk = false
            Exit For
        Else: bOk = true
        End If
Next

If bOk Then
    If MsgBox("Sheet is blank", vbOKCancel + vbInformation) = vbOK Then
    Worksheets("Main").Range("E29").Value = "NO"
    Cancel = True
    End If
End If

If bOk=false Then
    If MsgBox("Sheet is not blank", vbOKCancel + vbInformation) = vbOK Then
    Worksheets("Main").Range("E29").Value = "Yes"
    Cancel = True
    End If
End If

End If
End Sub

答案 2 :(得分:1)

当第一个单元格为空时,您似乎正在退出for循环,您只希望它在找到值时退出:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet

Dim Rvalue As Range
Dim cell As Range
Set Rvalue = Sheets("Uni-corp").Range("E10:G19")

If Worksheets("Main").Range("E29").Value = "YES" Then
For Each cell In Rvalue
        If IsEmpty(cell) Then
            bOk = True
            'Exit For moved to Else section
        Else: bOk = False
        Exit for
        End If
Next

If bOk Then
    If MsgBox("Sheet is blank", vbOKCancel + vbInformation) = vbOK Then
    Worksheets("Main").Range("E29").Value = "NO"
    Cancel = True
    End If
End If
End If
End Sub

答案 3 :(得分:1)

也许你是在追求这样的事情:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)    
    If UCase(Worksheets("Main").Range("E29").Value) <> "YES" Then Exit Sub
    If WorksheetFunction.CountA(Worksheets("Uni-corp").Range("E10:G19")) > 0 Then Exit Sub
    If MsgBox("Sheet is blank", vbOKCancel + vbInformation) = vbOK Then
        Worksheets("Main").Range("E29").Value = "NO"
        Cancel = True '<--| this will make the macro not save the workbook
    End If
End Sub