其他的答案工作,但效率較低,由於有逆轉的關鍵規則。沒有理由檢查一行是否爲空,除非其中一個必填字段丟失。從你的問題來看,規則是:
行必須包含所有必填字段,除非它們爲空。
這可以重新表述爲:
如果任何強制性字段爲空,整個行必須爲空。
第二的編程測試得多,高效得多,因爲你可以短路如果測試的第一部分是真實的:
Sub CheckRows()
Dim r As Long, c As Long, missingValue As Boolean
With ActiveSheet
For r = 1 To 5000
Select Case True
Case .Cells(r, "F") = vbNullString:
missingValue = True
Case .Cells(r, "G") = vbNullString:
missingValue = True
Case .Cells(r, "J") = vbNullString:
missingValue = True
Case Else
missingValue = False
End Select
'This is the ONLY CASE where you need to check if the row is empty.
If missingValue Then
If WorksheetFunction.CountA(.Range(.Cells(r, 1), .Cells(r, 33))) > 0 Then
MsgBox "Row " & r & " has some compulsory cells not filled in"
End If
End If
Next
End With
End Sub
使用[Range.AutoFilter方法]( https://msdn.microsoft.com/en-us/library/office/ff193884.aspx)將所有重要字段篩選爲空白。與Suntotal計數,看看它的過濾器發現任何東西,然後刪除它們。 – Jeeped
如果需要列並且沒有任何內容,那麼爲什麼你甚至不在乎行的其餘部分? – Comintern
@Comintern - 如果一個單元格中有內容,並且(例如)在該列上進行了SUM操作,則該總和可能無意義,如果該單元格只是垃圾數據而沒有在其他列中完成對應的必填字段。因此,如果非必填字段是必填字段,那麼檢查是否填寫了必填字段是非常合理的。 (或者反過來看,如果必填字段未完成,則不填寫非必填字段。) – YowE3K