+ Reply to Thread
Results 1 to 5 of 5

A for/next loop produces a negative number

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-03-2009
    Location
    Anchorage, Alaska, USA
    MS-Off Ver
    Office 365
    Posts
    126

    A for/next loop produces a negative number

    Hi,

    I'm using the dates as markers: Every date marks a new batch of data for that date; the data is below the date until the point a new date is found. In this example the marked cells are dates (rows 1, 21, 31) with data from each date listed below the date. Everything below 8/24 is from 8/24; everything from 9/2 is from 9/2 etc. Step one is defining these date-markers.

    Screenshot 2021-06-01 105654.jpg

    Ok, so here's my macro :

    Sub LoopTest1()
    Dim TNV As Integer
    Dim R As Integer
    Dim NR As Integer
    '
    NR = Cells(Rows.Count, 1).End(xlUp).Row
    TNV = 0
    '
    For R = 1 To NR
        TNV = TNV + IsDate(Range("a" & R))
        Cells(1, 4) = R
        Cells(1, 5) = TNV
        Next R
    '
    End Sub
    NR = find the last row of data
    R = for/next loop counter, which doubles as a row counter
    TNV = 'the new value' that is added to each time a date is found. Since IsDate is a TRUE/FALSE or 1/0 result, I simply added each IsDate result to the TNV running total.

    So I have it looking through column A while displaying the R and TNV values as the loop is running. The thing is TNV goes negative. TNV displays the correct number of dates (1, 3, 15 whatever), but in negative (-1, -3, -15 whatever).

    I'm stumped.

    Thank you.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,980

    Re: A for/next loop produces a negative number

    Quote Originally Posted by akedm View Post
    Since IsDate is a TRUE/FALSE or 1/0 result
    This assumption is incorrect. I caution against doing arithmetic with Boolean values, even though people love to do this in C. In VBA True casts to -1.

    The most reliable way to write this code is

    TNV = TNV + iif(IsDate(Range("a" & R)), 1, 0)
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,458

    Re: A for/next loop produces a negative number

    As 6SJ says, TRUE is not equal to 1

    To test:
    ?IsDate(Range("a" & R))
    True
    ?0+IsDate(Range("a" & R))
    -1 
    ?--IsDate(Range("a" & R))
    -1 
    ?-IsDate(Range("a" & R))
     1
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Contributor
    Join Date
    04-03-2009
    Location
    Anchorage, Alaska, USA
    MS-Off Ver
    Office 365
    Posts
    126

    Re: A for/next loop produces a negative number

    cause all 1s in bits = -1 ... and not-0 stuff ... got it.
    Thank you

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,458

    Re: A for/next loop produces a negative number

    You're welcome. Thanks for the rep.

+ 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. For loop produces duplicate results
    By naj1990 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2021, 06:14 AM
  2. [SOLVED] Make a cell use 0 when formula produces a negative number
    By rlowry in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-10-2017, 11:27 AM
  3. [SOLVED] Email a range of excel cells using VBA produces infinite loop HELP
    By kjam in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-13-2015, 09:58 PM
  4. [SOLVED] VBA Loop to check range for Negative number
    By Zarley in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2013, 01:58 PM
  5. Macro Loop until Negative number is reached and perform an interpolation
    By anaessens in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-16-2012, 09:54 AM
  6. Replies: 1
    Last Post: 11-20-2008, 01:52 AM
  7. [SOLVED] 2003= negative number&2004= negative number How Do I Calculate gro
    By Jason in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-14-2005, 02:06 PM

Tags for this Thread

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