【问题标题】:Show cells that are empty显示为空的单元格
【发布时间】:2018-02-07 13:43:22
【问题描述】:

我使用IsEmpty() 来确定单元格中是否有值,并在打印文件之前显示单元格缺少数据的消息。我希望能够通过显示一条消息来指定哪些单元格丢失,该消息指出哪些单元格没有数据,并且在所有字段都有值之前不让文件打印。

另外,除了显示没有值的单元格地址之外,是否可以在工作表中显示该单元格的定义名称?即C2 is NameF2 is Date....

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    If IsEmpty([C2]) Or IsEmpty([F2]) Or IsEmpty([K2]) Or IsEmpty([N2]) _
    Or IsEmpty([C3]) Or IsEmpty([A8]) Or IsEmpty([F8]) _
    Or IsEmpty([C34]) Or IsEmpty([C35]) _
    Or IsEmpty([C36]) Or IsEmpty([C37]) Or IsEmpty([G35]) _
    Or IsEmpty([G36]) Or IsEmpty([G37]) Or IsEmpty([I35]) _
    Or IsEmpty([I36] Or IsEmpty([I37]) _
    Or IsEmpty([L11]) Or IsEmpty([L18]) Or IsEmpty([L25]) _
    Or IsEmpty([J28]) Or IsEmpty([N28]) Then
        Cancel = True
        MsgBox ("Missing Cell. Please verify form!")
    End If
End Sub

【问题讨论】:

  • 欢迎来到 SO!请edit您的问题澄清。什么不完全有效?

标签: excel vba


【解决方案1】:

你可以试试这样的……

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim rng As Range, cell As Range
Dim EmptyFound As Boolean
Dim str As String
Set rng = Range("C2:C3,F2,K2,N2,A8,F8,L11,L18,L25,J28,N28,C34:C37,G35:G37,I35:I37")
str = "The following cells are empty. Please verify form!" & vbNewLine & vbNewLine
For Each cell In rng
    If IsEmpty(cell) Then
        EmptyFound = True
        str = str & cell.Address(0, 0) & vbNewLine
    End If
Next cell
If EmptyFound Then
    Cancel = True
    MsgBox str, vbExclamation, "Empty Cells Found!"
End If
End Sub

【讨论】:

  • 这工作完美!!谢谢,但不是显示丢失的单元格,是否可以为特定值添加一个类似“Missing Date”的字符串?
  • @RamiroMartinez 不客气。很高兴它按要求工作。我建议您打开一个新问题以及描述中的起始代码,并发布数据的屏幕截图和所需的输出。
  • 会的。再次非常感谢您。
【解决方案2】:

另一个镜头:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim arr
arr = Array("C2", "F2", "K2", "N2", "C3", "A8", "F8", "C34:C37", "G35:G37", "I35:I37")

With Worksheets("Sheet1") ' Change to your sheet.
    Dim rng As Range
    Dim i As Long
    For i = LBound(arr) To UBound(arr)
        If rng Is Nothing Then
            Set rng = .Range(arr(i))
        Else
            Set rng = Union(rng, .Range(arr(i)))
        End If
    Next i
    Dim rng2 As Range
    Set rng2 = .Range("A2:I37").SpecialCells(xlCellTypeBlanks)
    Dim oRange As Range
    Set oRange = Intersect(rng, rng2)
    If Not oRange Is Nothing Then
        MsgBox ("Missing Cell. Please verify form!") & vbNewLine & oRange.Address
        Cancel = True
    End If
End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    相关资源
    最近更新 更多