0
I would like to apply a check before executing next lines of code.
I wrote a simple structure like this, but it checks the first cell in range and if that cell is empty it goes to executing next step because of met requirements and the GoTo label.
What I need it to do is to check ALL of the cells, and proceed only if ALL are empty (not to go to next step after checking each cell individually). If one of them is not empty - the sub should exit.
Is it possible to do it using such a code or do I need a different approach in this case?
Sub Check_and_execute
Dim Cell As Range
For Each Cell In ThisWorkbook.Sheets("Sheet1").Range("A1:A10")
If Cell.Value = "" Then
GoTo NextStep
Else
MsgBox "Not all cells are empty."
GoTo EndSub
End If
Next
NextStep:
'code
EndSub:
End Sub
1Just wondering: Is a cell considered empty only if there is nothing in it? Or, as suggested by your code, would it still be considered empty if it contains a formula that returns a null string
""
? – Ron Rosenfeld – 2019-12-27T20:52:09.473Good point Ron. Application.CountA will count zero length strings (e.g.
""
) – Jeeped – 2019-12-28T09:22:20.187@RonRosenfeld Yeah, good point. In my case there are formulas in those cells that return a null string as you described. – Rayearth – 2019-12-29T11:24:15.247
@Rayearth Well, should those be considered empty? or not? if they are returning a null string. – Ron Rosenfeld – 2019-12-29T11:54:20.437
@RonRosenfeld Yes, those cells should be considered as empty. – Rayearth – 2019-12-29T17:07:27.053
@Rayearth See my suggestion with a single line of code to do the test. – Ron Rosenfeld – 2019-12-30T02:34:33.237