Reputation: 129
I am doing a macro that checks whether cells are empty or full. But is there any fast way to check if only one cell out of three, in a row, is not empty?
my code:
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
But is there a way to check if only one two or three out of 4 cells are not empty?
I am looking for. In my code i would like it to check the following:
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"
So if 2 are empty and two are not it shpuld always check it. I dont want to write a lot of if statements that is why i am asking if there is any faster way-
Upvotes: 0
Views: 751
Reputation: 6659
As per your sample code your objective is to identify when:
Suggest the use of objects, also to marked (either with color or with a value in an adjacent cell) the cells found. Below you have two sets of codes one showing a message for each row with full values or totally empty (as you have now) and also a sample with the suggestion of coloring the resulting cells.
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
Upvotes: 0
Reputation: 96781
For a specific set of cells, A1 through D1
One way:
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
Here we explicitly ignore the case of Null strings.
Upvotes: 3