+ Reply to Thread
Results 1 to 11 of 11

detecting zero length vs null vs. long conversions

  1. #1
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    detecting zero length vs null vs. long conversions

    I know I've asked something similar to this before you guys, but Excel is seriously annoying me right now. Can anyone tell me why this code is printing EVERY single row in a range of 50000 lines in column 'E'!? At the bottom of this post is an image of the sheet I'm working with. I don't see anything wrong with it myself.
    Please Login or Register  to view this content.
    Attached Images Attached Images

  2. #2
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2024
    Posts
    6,227

    Re: detecting zero length vs null vs. long conversions

    Try this:

    Please Login or Register  to view this content.

  3. #3
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: detecting zero length vs null vs. long conversions

    Thanks I will try it when I get home but can I ask you why exactly does mine not work?

  4. #4
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: detecting zero length vs null vs. long conversions

    Not IsNull(r) ?

    I believe you want Not IsEmpty(r.Value) .

    From VBA Help with emphasis added: The Null value indicates that the Variant contains no valid data. Null is not the same as Empty, which indicates that a variable has not yet been initialized. It is also not the same as a zero-length string (""), which is sometimes referred to as a null string.

    Gettin' picky: if Trim(r) <> "" then Len(r) <> 0, meaning you don't need both. For that matter, IsNumeric(Left(r, 1)) should be sufficient to check that the 1st char in r.Value is a decimal numeral.

  5. #5
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: detecting zero length vs null vs. long conversions

    Thanks I appreciate it and based on the help section of the health article you posted for me to look at, it's talking about variables and that's not really relevant in this case because I am checking the actual worksheet value in the cells I'm not checking a variable in code. But regardless, yes you are right and that I want to check empty. And the only reason that I have nested three functions inside the check for the outermost function called isnumeric,is because I've run into issues with this language a million times over 20 years and it's proven to me time and time again that it's completely unreliable in terms of what functions do what and what functions to return what types of objects.

    And it's because of that reality that I write code in such a way that I don't take a chance on whether VBA can decipher what I want as a return so if I want to string returned and the value I'm checking is technically a numeric value even though it's left aligned in the cell and formatted as a string, I will still use the string conversion function.

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,496

    Re: detecting zero length vs null vs. long conversions

    Quote Originally Posted by Phuocam View Post
    Try this:

    Please Login or Register  to view this content.
    I don't think you need the Len test as I believe [!0-9] cannot be the empty string. You can also test r.value directly by using a wild card. If I am correct, this should do the same thing as our code line above...
    Please Login or Register  to view this content.
    Edit Note: Just added the Trim function call in response to hrlngrv comment in Message #8.
    Last edited by Rick Rothstein; 10-20-2020 at 10:36 AM.

  7. #7
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: detecting zero length vs null vs. long conversions

    OK, but another thing I missed before is that if you need to call Trim first, then you should also call Trim inside the IsNumeric call.

    I've been writing VBA code since the mid-1990s, before that XLM and Lotus 1-2-3 macros in addition to other languages. I haven't come across the type of inconsistency you mention. Something like the following has always worked for me AS LONG AS all spaces are ASCII spaces rather than HTML nonbreaking spaces or other exotica.

    Please Login or Register  to view this content.
    If the source of the values are cell values, always best to ensure you're not dealing with an error value.

    Note that for blank cells, LTrim$(x) returns "".

    The only thing I know of which would screw this up would be characters which appear to be spaces which aren't ASCII spaces at the beginning of cell values. That's a data cleansing exercise.

  8. #8
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: detecting zero length vs null vs. long conversions

    Presumably the Trim call is there for a reason.

  9. #9
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: detecting zero length vs null vs. long conversions

    hey guys,

    is there a difference between r and r.value?? I've never noticed any difference whatsoever. not when reading range values anyway.

  10. #10
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,496

    Re: detecting zero length vs null vs. long conversions

    The default property when not specified is Value, so with r declared as a Range variable, r and r.value are the same when the situation you are using it in expects a value.

  11. #11
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: detecting zero length vs null vs. long conversions

    Quote Originally Posted by Rick Rothstein View Post
    . . . comment in Message #8.
    These message numbers depend on the type of message threading one's using. Using the type I've selected, my message appears as #4.

+ 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: 1
    Last Post: 09-16-2020, 11:09 PM
  2. NULL vs EMPTY vs 0-length string
    By vba_php in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-30-2020, 09:42 PM
  3. Macro about detecting null and something else
    By ridleyyeh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-01-2019, 05:28 AM
  4. Length of the longest consecutive data series with values not null
    By jacknobody in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-24-2013, 09:42 AM
  5. Automatically detecting data length for graph
    By willist in forum Excel General
    Replies: 2
    Last Post: 07-19-2012, 02:35 PM
  6. Splting a long string, at the spaces, with a max length of 24
    By bassima in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-15-2011, 02:26 AM
  7. Lat & Long conversions
    By LBCMH in forum Excel General
    Replies: 7
    Last Post: 04-07-2008, 12:36 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