0

我需要excel的vba代码。我正在检查 A1、B7、C9 是否为空 onclick。

如果它们为空(任何或所有变量为空),我需要返回:
“是如此如此,所以单元格为空,单击确定填充它”

如果没有一个单元格为空,或者所有单元格都不为空,或者它们包含任何值,我需要返回:
“Do my stuff”

有一个特定工作簿的链接,但我也想在这里查看。

4

4 回答 4

1
Sub tgr()

    Dim CheckCell As Range

    For Each CheckCell In Sheets("Sheet1").Range("A1,B7,C9").Cells
        If Len(Trim(CheckCell.Value)) = 0 Then
            CheckCell.Select
            MsgBox "Cell " & CheckCell.Address(0, 0) & " is empty. Click OK and populate it.", , "Missing Information"
            Exit Sub
        End If
    Next CheckCell

    'All cells filled, code to Do My Stuff goes here
    MsgBox "Do my stuff", , "Continue macro"

End Sub
于 2013-10-11T19:59:06.973 回答
1

如果您关心单元格是否真的为空,则需要对 Value 属性使用 IsEmpty 函数。对于具有单个撇号的单元格或返回空字符串的函数,这将返回 false。

Public Function CellsAreEmpty(ParamArray aCells() As Variant) As Boolean

    Dim vItm As Variant
    Dim bReturn As Boolean

    bReturn = True

    For Each vItm In aCells
        bReturn = bReturn And IsEmpty(vItm.Value)
    Next vItm

    CellsAreEmpty = bReturn

End Function


Sub TestCells()

    If CellsAreEmpty(Range("A1"), Range("B7"), Range("C9")) Then
        Debug.Print "Do stuff"
    Else
        Debug.Print " is so so so cell is empty click ok to fill it"
    End If

End Sub
于 2013-10-11T20:27:02.890 回答
0

是你要找的吗?

下面的代码将检查 sheet1- Range A1, B7, c9 。

Sub checkEmptyCells()

    With ThisWorkbook.Sheets("sheet1")
        If (Len(.Range("A1")) = 0) Then
            MsgBox "Cell A1 is empty. Click ok to fill"
            Exit Sub
        ElseIf (Len(.Range("B7")) = 0) Then
            MsgBox "Cell B7 is empty. Click ok to fill"
            Exit Sub
        ElseIf (Len(.Range("C9")) = 0) Then
            MsgBox "Cell C9 is empty. Click ok to fill"
            Exit Sub
        Else
            MsgBox "Do my stuff"
            Exit Sub
        End If

    End With

End Sub
于 2013-10-11T19:23:04.207 回答
0

如果您更喜欢使用非 vba 解决方案,那么您可以使用条件格式。它不会给出消息框,但会在单元格为空白时突出显示它们。

要使用条件格式,请按照以下步骤操作

  • 选择单元格
  • 在 Home 选项卡 > Sytles > 条件格式下
  • 将出现条件格式对话框。单击新规则按钮。
  • 选择使用公式确定要格式化的单元格。
  • 输入公式(如果选择单元格 A1,则 A1="")
  • 单击格式>转到填充,然后选择所需的颜色。

在此处输入图像描述

于 2013-10-11T20:47:16.880 回答