+ Reply to Thread
Results 1 to 8 of 8

Proper syntax to exit a loop if IsError is true

  1. #1
    Registered User
    Join Date
    10-17-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2007
    Posts
    79

    Proper syntax to exit a loop if IsError is true

    Hi,

    As the title says it, I am currently stuck with a specific part of a project ( I am still new at programming and VBA).

    My final goal is to count how many words are contained in each cell of a column.
    To do that, and for 1 cell, my method is to count how many spaces there are, and to say that there is 1 more word.
    To count these spaces, I want to use a loop which finds the first space, then the next one by starting at the location of the previous one +1

    The procedure attached woks until there is no space left to find. Then I get an error message.
    Any help to straighten up this coding would be very much appreciated.

    Thank you for reading.

    The attached file contains a procedure which purpose is to count the words in C4
    IsError_WordsPerCell-2ndMethod.xlsm

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Proper syntax to exit a loop if IsError is true

    take a look at the SPLIT functon

    Please Login or Register  to view this content.
    untested but I believe this should work....you don't have to check through the cell for spaces the Split command "splits" the Value by whatever delimiter you use and puts it in an array and then you can use Ubound (Upper Bound) + 1 to "count" the spaces....HTH
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Proper syntax to exit a loop if IsError is true

    here is your code with my changes....

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Proper syntax to exit a loop if IsError is true

    Formula:

    Please Login or Register  to view this content.
    VBA
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-17-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: Proper syntax to exit a loop if IsError is true

    Sorry for not answering earlier.
    Thank you to both for your answers; they have been very valuable and instructive. I am always glad when I learn something else that what I was requesting
    You have provided 2 different solutions to my problem, but they don't answer my question, which was about the proper syntax of IsError to have it working as intended within the loop. I can't mark the thread as Solved for this reason, but I am going to add to your reputation for the value of your help.
    Thank you again!

  6. #6
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Proper syntax to exit a loop if IsError is true

    I don't know why your IsError system isn't working...everything I look up shows that it uses a IsError(RtnValue) and the RtnValue is set from an expression (IE: a valid error number), so in theory it should work...but I wouldn't use this looping technique for this application....I believe both stnkynts and I have shown a different and possibly better way...I totally understand your wanting to understand why this doesn't work and I can only give a weak answer....sorry....HTH

    Edit: the IsError didn't show in the actually looping construct....not that it can't, I just haven't seen it used that way....
    Last edited by judgeh59; 06-30-2014 at 12:46 PM.

  7. #7
    Registered User
    Join Date
    10-17-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: Proper syntax to exit a loop if IsError is true

    Thank you for answering back. I am too new to VBA/programming to realize if what I am doing should be done one way or another one.

    I have used your code for my project; I found it very clean and it works perfectly. Even after that, I have tried to understand why my loop doesn't work, trying several variations, but with no effect. I am just giving up

    PS: I am not sure that I understand the first part of your Edit.

  8. #8
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Proper syntax to exit a loop if IsError is true

    no problem on the "not understanding"...sometimes it works that way....in doing a little research I didn't see any examples that used the IsError like you were using it. That's all I was trying to say.

    Excellent realization that there are usually at least 2 ways of doing something in XL and keeping an open mind really helps....kudo's to you....

    I'm not sure why your code didn't work either, except, again, I haven't seen IsError used in that way...hope this helps....

+ 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. Proper IF syntax when using in VBA
    By scott28 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-27-2013, 06:25 AM
  2. =IF(ISERROR(V6/AJ6)=TRUE,0,V6/AJ6) Please help!!
    By Rick Teo in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-25-2013, 03:09 AM
  3. Issue with IF(ISERROR(VLOOKUP syntax
    By jh9 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-20-2011, 12:45 PM
  4. Proper use of ISERROR() formula
    By Pyrex238 in forum Excel General
    Replies: 3
    Last Post: 04-11-2011, 07:04 PM
  5. Is this proper syntax?
    By Shelby Haynes in forum Excel General
    Replies: 3
    Last Post: 05-10-2005, 05:06 AM

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