如果数据丢失,请检查多个单元格和消息框

时间:2017-03-16 12:13:49

标签: excel vba message

有没有办法检查多个范围,如果它们是空白的,则显示一个消息框并告诉我哪些数据丢失了?目前我正在打字并做了很多不同的事情。想知道是否有更简单的方法吗?

我的范围是:范围(“C11:D11,F11:G11,I11:J11,C14:F14,I14:J14,C15:F15,I15:J15,B18:J18,B42:J42”))

If WorksheetFunction.CountA(Range("C9:E9")) = 0 Then
 Worksheets("Create Form").Range("C9:E9").Select
 MsgBox "Please enter information in the required fields."
 Exit Sub
End If

If WorksheetFunction.CountA(Range("H9:J9")) = 0 Then
 Worksheets("Create Form").Range("H9:J9").Select
 MsgBox "Please enter information in the required fields."
 Exit Sub
End If

谢谢

1 个答案:

答案 0 :(得分:0)

有一种方法可以检查多个范围,在下面的代码中,通过在该范围的所有For中使用Areas循环来实现。

然后对每个CurRng(重新发布一个区域),检查是否WorksheetFunction.CountA(CurRng) = 0

<强>代码

Option Explicit

Sub CheckMultipleRanges()

Dim MyMultiRng      As Range
Dim CurRng          As Range

With Worksheets("Create Form")   
   ' use a variable to set the multiple ranges in it 
   Set MyMultiRng = .Range("C11:D11,F11:G11,I11:J11,C14:F14,I14:J14,C15:F15,I15:J15,B18:J18,B42:J42")

   ' loop through all areas in the multi-range
    For Each CurRng In MyMultiRng.Areas
        If WorksheetFunction.CountA(CurRng) = 0 Then
            CurRng.Select
            MsgBox "Please enter information in the required fields."
            Exit Sub
        End If
    Next CurRng
End With

End Sub