I don't know if it's possible, but I want to check if a range in Excel is empty. So how do I write if:
Range("A38":"P38")
Is empty in VBA code?
Thanks in advance.
I don't know if it's possible, but I want to check if a range in Excel is empty. So how do I write if:
Range("A38":"P38")
Is empty in VBA code?
Thanks in advance.
Found a solution from the comments I got.
From experience I just learned you could do:
Clarification to be provided a bit later (right now I'm working)
Another possible solution. Count empty cells and subtract that value from the total number of cells
If you find yourself in a situation where it's absolutely necessary that you loop through each cell in a range instead of using
CountA
, then it's much faster to first pull that range of data into an array and loop on that array's values instead of the cells' values themselves. You can send this function a range to get back a true/false result.Example of how to use it:
If
Range("A38:P38")
is empty, it would printTrue
; otherwise it'd printFalse
.This will return
True
if no cells inSelection
contains any data. For a specific range, just substituteRANGE(...)
forSelection
.IsEmpty returns True if the variable is uninitialized, or is explicitly set to Empty; otherwise, it returns False. False is always returned if expression contains more than one variable. IsEmpty only returns meaningful information for variants. (https://msdn.microsoft.com/en-us/library/office/gg264227.aspx) . So you must check every cell in range separately:
Of course here are more code than in solution with CountA function which count not empty cells, but GoTo can interupt loops if at least one not empty cell is found and do your code faster especially if range is large and you need to detect this case. Also this code for me is easier to understand what it is doing, than with Excel CountA function which is not VBA function.