检查单元格是否为空

我正在做一个macros,检查单元格是空的还是满的。 但有没有什么快速的方法来检查,如果连续三个中的一个单元格不是空的?

我的代码:

LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row ThisWorkbook.Sheets(1).Range("A1").Select Do Until ActiveCell.row = LastRow + 1 If IsEmpty(ActiveCell) = False Then If IsEmpty(Cells(ActiveCell.row, 1)) = False And IsEmpty(Cells(ActiveCell.row, 1)) = False And IsEmpty(Cells(ActiveCell.row, 3))=False And IsEmpty(Cells(ActiveCell.row, 4))=False Then MsgBox "None empty empty" ElseIf IsEmpty(Cells(ActiveCell.row, 1)) = True And IsEmpty(Cells(ActiveCell.row, 2)) = True And IsEmpty(Cells(ActiveCell.row, 3)) = True And IsEmpty(Cells(ActiveCell.row, 4)) = True Then MsgBox "All empty" End If End If ActiveCell.Offset(1, 0).Select Loop 

但有没有办法来检查,如果只有四个单元格中的两个或三个不是空的?

我在寻找。 在我的代码,我希望它检查以下内容: If IsEmpty(Cells(ActiveCell.row, 1)) = False And IsEmpty(Cells(ActiveCell.row, 1)) = False And IsEmpty(Cells(ActiveCell.row, 3))=True And IsEmpty(Cells(ActiveCell.row, 4))=True Then MsgBox "2 empty"

所以,如果2是空的,两个不是它shpuld总是检查它。 我不想写很多如果陈述,这就是为什么我问是否有更快的方式,

对于一组特定的单元格,从A1D1

单程:

 Sub EmptyCounter() Dim rng As Range Dim wf As WorksheetFunction Set wf = Application.WorksheetFunction Set rng = Range("A1:D1") MsgBox "There are " & 4 - wf.CountA(rng) & " empties" End Sub 

这里我们明确地忽略空string的情况。

根据您的示例代码,您的目标是确定何时:

  1. 所有单元格中的第一个单元格是空的或
  2. 所有单元格中的第一个单元格不是空的
  3. 行与空和非空单元混合将被忽略

build议使用对象,也可以标记(或者用颜色或者在相邻单元格中的值)find的单元格。 在下面你有两组代码,每组显示一个消息,每行有完整值或完全空白(如你现在),还有一个样本,build议着色结果单元格。

 Rem Code showing messages Sub Wsh_MarkCellsEmptyAndNotEmpty_Msg() Dim RngTrg As Range Dim lRowLast As Long Dim vCellsValue As Variant Dim lRow As Long Dim bNoneEmpty As Byte Dim b As Byte Rem No changes to your method for finding last row lRowLast = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row Rem Set Target Range Set RngTrg = ThisWorkbook.Sheets(1).Range(Cells(1), Cells(lRowLast, 4)) For lRow = 1 To lRowLast With RngTrg.Rows(lRow) Rem To Select cells [NOT RECOMMENDED PRACTICE] Rem Instead suggest to marked cells found .Select Rem Initiate Variables bNoneEmpty = 0 vCellsValue = Empty Rem Look into cells values For b = 1 To 4 If .Cells(b).Value <> Empty Then bNoneEmpty = 1 + bNoneEmpty vCellsValue = vCellsValue & .Cells(b).Value2 Next Rem Show Message with Results If vCellsValue = Empty Then MsgBox "All Cells are empty" ElseIf bNoneEmpty = 4 Then MsgBox "None Cell is empty" End If End With: Next End Sub Rem Code marking cells with color (user friendly) Sub Wsh_MarkCellsEmptyAndNotEmpty_Color() Dim RngTrg As Range Dim lRowLast As Long Dim vCellsValue As Variant Dim lRow As Long Dim bNoneEmpty As Byte Dim b As Byte Rem No changes to your method for finding last row lRowLast = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row Rem Set Target Range Set RngTrg = ThisWorkbook.Sheets(1).Range(Cells(1), Cells(lRowLast, 4)) Rem To Clear Cells Colors if marking with colors cells found RngTrg.Interior.Pattern = xlNone For lRow = 1 To lRowLast With RngTrg.Rows(lRow) Rem Initiate Variables bNoneEmpty = 0 vCellsValue = Empty Rem Look into cells values For b = 1 To 4 If .Cells(b).Value <> Empty Then bNoneEmpty = 1 + bNoneEmpty vCellsValue = vCellsValue & .Cells(b).Value2 Next Rem Mark Resulting cells If vCellsValue = Empty Then Rem Colors Empty Cells in Red .Interior.Color = RGB(255, 199, 206) ElseIf bNoneEmpty = 4 Then Rem Colors No Empty Cells in Green .Interior.Color = RGB(198, 239, 206) End If End With: Next End Sub