PDA

View Full Version : How to check if a cell contains something or not



Geetha Gupta
01-19-2017, 04:54 AM
A cell contains a formula that evaluates to 0

But the following code shows the cell as empty ie.. it shows Range("A6") as empty whereas the cell has formula that results into zero


If Range("A5").Offset(1, 0) = Empty Then 'if the cell below is empty

hence the sub chooses the wrong branch while executing.

how can this error be corrected

Thank you in advance
regards,
Geetha

Geetha Gupta
01-19-2017, 06:29 AM
This works though i fail to understand the difference


If IsEmpty(Range("A5").Offset(1, 0)) Then

Regards
Geetha

gtbreps_2
01-19-2017, 07:25 AM
Geeta,
Try



If Range("A5").Offset(1,0)).HasFormula Then
'Code if cell has a formula
Else
'Code if cell doesn't have a formula
End If

Paul_Hossler
01-19-2017, 08:53 AM
The Empty keyword is used as a Variant subtype. It indicates an uninitialized variable value.





IsEmpty( expression ) The required expression argument (https://msdn.microsoft.com/en-us/library/office/gg264568.aspx) is a Variant (https://msdn.microsoft.com/en-us/library/office/gg264568.aspx) containing a numeric (https://msdn.microsoft.com/en-us/library/office/gg264568.aspx) or string expression (https://msdn.microsoft.com/en-us/library/office/gg264568.aspx). However, because IsEmpty is used to determine if individual variables are initialized, the expression argument is most often a single variable name.

Remarks
IsEmpty returns True if the variable is uninitialized, or is explicitly set to Empty (https://msdn.microsoft.com/en-us/library/office/gg264568.aspx); 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/gg264568.aspx).


'Empty' and 'IsEmpty' might not be the best way

What are you trying to determine?

Aussiebear
01-19-2017, 02:21 PM
I don't understand why you are testing to see if the cell is empty when ( to my way of thinking), you should be testing if the cell is blank.