Results 1 to 3 of 3

Thread: [RESOLVED] VBA incorrectly entering if statement that is false

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    Resolved [RESOLVED] VBA incorrectly entering if statement that is false

    I attached the excel spreadsheet that I'm working in. This procedure goes through each row and checks to see if the fourth column >= (the second column + 0.01) and if it is then it highlights it yellow.

    The problem I'm having is on row 74. The value of the fourth column is 0.821 and (the second column + 0.01)= 0.821 (I even verified these values in debug mode). However, it continues to go into the first if statement in red text below rather than the ElseIf right below it. Anyone have any suggestions?


    Code:
    Public Const ReliabMarg = 0.01
    
    Sub reliab()
        Dim ReliabLimit As Double
        Range("A1").Activate
        Do Until IsEmpty(ActiveCell) = True And IsEmpty(ActiveCell.Offset(1, 0)) = True
            If IsEmpty(ActiveCell) Then
                ActiveCell.Offset(1, 0).Activate
            ElseIf IsNumeric(ActiveCell.Offset(0, 1)) = False Then
                ActiveCell.Offset(1, 0).Activate
            ElseIf IsNumeric(ActiveCell.Offset(0, 1)) = True Then
                ReliabLimit = ActiveCell.Offset(0, 1).Value + ReliabMarg
                If ActiveCell.Offset(0, 3).Value < ReliabLimit Then
                    ActiveCell.Offset(1, 0).Activate
                ElseIf ActiveCell.Offset(0, 3).Value >= ReliabLimit Then
                    ActiveCell.Offset(0, 3).Interior.ColorIndex = 6
                    ActiveCell.Offset(1, 0).Activate
                End If
            End If
        Loop
    End Sub
    Attached Files Attached Files
    Last edited by mansfin; Mar 5th, 2012 at 03:24 PM.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,541

    Re: VBA incorrectly entering if statement that is false

    " Dim ReliabLimit As Double" <- thar be your problem... Doubles are floating point and so are notoriously inaccurate. Floating points are not stored by their exact value, but rather an approximation... so while you see 0.821, it's probably 0.8209999999999999999999999999999999999999 ...

    you'll need to make sure you're comparing apples to apples... round both sides of the equation to the same number of digits... Sucks I know, but I've seen 1 turn into 0.9999999999999999999999999999999 before.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    Re: VBA incorrectly entering if statement that is false

    Thank you much that worked very well.

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