2017-03-16 32 views
0

有沒有辦法檢查多個範圍,如果它們是空白的,然後顯示一個消息框並告訴我哪些數據缺失?目前,我正在輸入這些內容並執行大量不同的操作。想知道是否有更簡單的方法? (「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 

謝謝

回答

0

有一種方法來檢查多個範圍,並在它下面的代碼是通過使用通過系列的所有Areas一個For循環來實現。

然後爲每個CurRng(represnting一個區域),檢查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 
+0

你是個明星:)謝謝 –

+0

對不起只注意到它不能夠在錯誤發生時我強調的細胞。有沒有一種方法可以選擇單元格,並且是否有方法將消息框與它合併? –

+0

@ShawnCartwright你看到我的代碼了嗎? 「其餘的代碼放在這裏」,給我5和是的 –