Results 1 to 6 of 6

Thread: check cells before division by zero

  1. #1

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    check cells before division by zero

    I want to make sure of no divison by zero error messages
    I am doing division on a mshflexgrid like this:
    with grid
    .textmatrix(2, 12) = .textmatrix(2, 11) / .textmatrix(2, 9)
    some of the cells maybe empty and some maybe formated as currency with a dollar sign
    is this the best method:
    vb Code:
    1. If val(.textmatrix(2, 11) <> 0 then
    2.       If val(.textmatrix(2, 9) <> 0 then
    3.              .textmatrix(2, 12) = .textmatrix(2, 11) / .textmatrix(2, 9)
    4.       else
    5.              textmatrix(2, 12) = ""
    6.       end if
    7. end if
    would this code be appropriate?
    Last edited by Hack; Mar 8th, 2007 at 02:48 PM. Reason: Added VB Highlight Tags and Indenting

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: check cells before division by zero

    Quote Originally Posted by isnoend07
    would this code be appropriate?
    It looks Ok. Does it work? Are you havinga problem?

  3. #3

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: check cells before division by zero

    Quote Originally Posted by Hack
    It looks Ok. Does it work? Are you havinga problem?
    Yes i am having having a problem, but i am not sure if this is it.
    I have a huge program with about 50 of these. It has gotton very hard to
    debug. Many are if this chbox is checked then divide by this cell if not divide by this cell etc, etc....

  4. #4

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: check cells before division by zero

    Quote Originally Posted by isnoend07
    Yes i am having having a problem, but i am not sure if this is it.
    I have a huge program with about 50 of these. It has gotton very hard to
    debug. Many are if this chbox is checked then divide by this cell if not divide by this cell etc, etc....

    After experimenting i have found this code will fail if val(contents) is formatted as currency. It will return 0 even though it contains a valid number

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: check cells before division by zero

    Consider using the INSTR function to determine if any number other than zero exists in the cell string. If it does, you can then next pinpoint the starting position of the first non-zero integer and determine the value of the midstring using Val from that point on.

    It's rather amazing what we have to do sometimes to evaluate strings.
    Doctor Ed

  6. #6

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: check cells before division by zero

    Quote Originally Posted by Edwin Hackleman
    Consider using the INSTR function to determine if any number other than zero exists in the cell string. If it does, you can then next pinpoint the starting position of the first non-zero integer and determine the value of the midstring using Val from that point on.

    It's rather amazing what we have to do sometimes to evaluate strings.
    I have decided to use currency only for totals and not subtotals. Saves a lot of ifs

Posting Permissions

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



Click Here to Expand Forum to Full Width