+ Reply to Thread
Results 1 to 9 of 9

Empty string vs cell format

  1. #1
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Empty string vs cell format

    If I test for the empty string on a cell that is empty and its format is (default) general or text, I would expect this test to be true. However, if I change the format of the empty cell to number format, how come it still succeeds? It seems like it should fail because the cell isn't actually a string type but a number type. Similarly, if I do an if 2 = "2", this also works and this should through an error I would think. How is Excel interpreting these types of comparisons?

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,069

    Re: Empty string vs cell format

    An empty cell is just that, empty. The format simply changes the way a cell is viewed, it does not change the value(if any) of the cell.
    So if A1 is empty then =ISBLANK(A1) will return true regardless of the format.
    I don't understand what you mean by
    Similarly, if I do an if 2 = "2", this also works

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,809

    Re: Empty string vs cell format

    Excel formulas do not look at cell formats, but the value contained in the cell. Any blank cell (assuming truly blank and not some formula that returns "") will be interpreted as 0 or "", depending on the formula doing the comparison. So if A1 is blank, =A1="" and =A1=0 will both return TRUE -- and the number format will not impact this result.

    I'm not sure what you mean by 2="2" works. When I put =2="2" into a cell, I get FALSE, because Excel sees the number 2 as something different than the text string "2". If I format a pair of cells as "0.00" and enter 2 into one and '2 (text 2) into another, and enter =A1=A2, I also get FALSE. The number format does not change how Excel evaluates the resulting boolean expression.

    I don't know if it helps, but I think a lot of people get confused by this when dates are involved. Enter the number 42308 into a pair of cells and format one as a date format ("m/d/yy" maybe). =A1=A2 will still return TRUE, even though one cell is formatted as a date and the other is formatted as a number.

    The key point is that Excel does not use number formatting when deciding how to evaluate formulas. It uses the value stored in the cell. Empty is interpreted as either 0 or "", depending on the formula. Does that help answer your question?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Empty string vs cell format

    Quote Originally Posted by MrShorty View Post
    Excel formulas do not look at cell formats, but the value contained in the cell. Any blank cell (assuming truly blank and not some formula that returns "") will be interpreted as 0 or "", depending on the formula doing the comparison. So if A1 is blank, =A1="" and =A1=0 will both return TRUE -- and the number format will not impact this result.

    I'm not sure what you mean by 2="2" works. When I put =2="2" into a cell, I get FALSE, because Excel sees the number 2 as something different than the text string "2". If I format a pair of cells as "0.00" and enter 2 into one and '2 (text 2) into another, and enter =A1=A2, I also get FALSE. The number format does not change how Excel evaluates the resulting boolean expression.

    I don't know if it helps, but I think a lot of people get confused by this when dates are involved. Enter the number 42308 into a pair of cells and format one as a date format ("m/d/yy" maybe). =A1=A2 will still return TRUE, even though one cell is formatted as a date and the other is formatted as a number.

    The key point is that Excel does not use number formatting when deciding how to evaluate formulas. It uses the value stored in the cell. Empty is interpreted as either 0 or "", depending on the formula. Does that help answer your question?
    MrShorty - Thank you this was helpful. What I meant by 2 = "2" is that if you use an If statement in VBA comparing those two values, it returns true. Shouldn't that throw an error because you cannot compare two different data types or does it convert one to the other in order to compare them? I would think other stronger typed languages would give a compile time error for this.

  5. #5
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Empty string vs cell format

    Quote Originally Posted by Fluff13 View Post
    An empty cell is just that, empty. The format simply changes the way a cell is viewed, it does not change the value(if any) of the cell.
    So if A1 is empty then =ISBLANK(A1) will return true regardless of the format.
    I don't understand what you mean by
    Fluff13 - Thank you. See below regarding my comment about 2 = "2".

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,069

    Re: Empty string vs cell format

    You should not get a compile error when comparing different values, you will simply get a boolean value.
    If you enter 2 into A1 & '2 into B1 & run this
    Please Login or Register  to view this content.
    It will return FALSE as one is a number & the other is text.

  7. #7
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Empty string vs cell format

    Quote Originally Posted by Fluff13 View Post
    You should not get a compile error when comparing different values, you will simply get a boolean value.
    If you enter 2 into A1 & '2 into B1 & run this
    Please Login or Register  to view this content.
    It will return FALSE as one is a number & the other is text.
    Hmmm, if you step through the code below, it returns true...
    Please Login or Register  to view this content.

  8. #8
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,069

    Re: Empty string vs cell format

    That's true, although I don't know why, but why would you want to do that?

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,809

    Re: Empty string vs cell format

    I missed that this was in VBA. Note that VBA -- since it is a different programming language -- has different rules for how it interprets comparisons to different data types. Where Excel does little to try to coerce the text "2" to the number 2 before performing the comparison, I think VBA will put more effort, so to speak, into the comparison. It sees that the only difference between 2 and "2" is the data type, but that the test can return True, if one value is converted to the other's data type. Of course, there is another layer there if you are comparing a VBA variable to a cell value (and maybe even different for the .Value, .Value2, and .Text properties of a cell).

    I don't know where any of this kind of stuff is documented. I know that I frequently just try and test things in debug mode to see how VBA behaves in a given situation.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 01-02-2015, 08:57 PM
  2. [SOLVED] If cell string does not contain a number make cell empty
    By capson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-06-2014, 08:32 PM
  3. [SOLVED] Conditional String Concatenation based on different cell being empty or not
    By MrGadget6977 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-16-2013, 01:14 PM
  4. Find a cell that has a null value, and replace with an empty string.
    By skania in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-20-2013, 12:49 PM
  5. [SOLVED] Adding a button to find string in column and fill out the first empty cell
    By vaznlyfe in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-27-2012, 04:16 PM
  6. How not to display a string from an empty cell
    By Soooooz01 in forum Excel General
    Replies: 5
    Last Post: 03-26-2012, 08:32 AM
  7. [SOLVED] Copy a string text to cell if another cell not empty
    By Pasmatos in forum Excel General
    Replies: 0
    Last Post: 11-25-2005, 12:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1