我正在做一个宏来检查单元格是空还是满。但是有没有快速的方法来检查连续三个中只有一个单元格是否为空?
我的代码:
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
但有没有办法检查4个单元格中只有一个或两个不是空的?
我在找。在我的代码中,我希望它检查以下内容:
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总是检查它。我不想写很多if语句,这就是为什么我在问是否有更快的方法 -
答案 0 :(得分:3)
对于一组特定的单元格, A1 到 D1
一种方式:
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
这里我们明确地忽略了Null字符串的情况。
答案 1 :(得分:0)
根据您的示例代码,您的目标是确定何时:
建议使用对象,也用于标记(使用颜色或相邻单元格中的值)找到的单元格。 下面有两组代码,其中一行显示每行的全部值或完全为空(如您所知),还有一个示例,其中包含着色结果单元格的建议。
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