+ Reply to Thread
Results 1 to 6 of 6

If statement not adding up correctly

Hybrid View

  1. #1
    Registered User
    Join Date
    03-16-2010
    Location
    Chesterfield, England
    MS-Off Ver
    Excel 2016
    Posts
    19

    If statement not adding up correctly

    Hiya all.

    I am writing a macro to look a value in one column (Difference) which then adds one onto the appropriate variable. It is semi working, but the macro is adding what should be going on the P and M variable and is adding it on to the W variable.

    Am i doing something completly silly? I cant seem to figure out what the problem is.

    Thanks for any help.


    If LOPNum > 0.5 Then
        PP = PP + 1
        Else
        If LOPNum = 0.4 Then
            P = P + 1
            Else
                If LOPNum = 0.3 Then
                P = P + 1
                Else
                    If LOPNum = 0 Then
                    W = W + 1
                    Else
                        If LOPNum = -0.3 Then
                        M = M + 1
                        Else
                            If LOPNum = -0.4 Then
                            M = M + 1
                            Else
                                If LOPNum <= -0.6 Then
                                MM = MM + 1
                                End If
                            End If
                        End If
                    End If
                End If
        End If
    End If
    Last edited by sted999; 07-19-2010 at 10:54 AM.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: If statement not adding up correctly

    To make your code easier to follow, try using a Select statement rather than nested Ifs.
    This is your code converted with no refinements
        Select Case LOPNum
            Case Is > 0.5
                PP = PP + 1
            Case 0.4
                P = P + 1
            Case 0.3
                P = P + 1
            Case 0
                W = W + 1
            Case -0.3
                M = M + 1
            Case -0.4
                M = M + 1
            Case Is <= -0.6
                MM = MM + 1
        End Select

    Without the rest of your code it isn't clear what you are actually trying to do
    ...., but the macro is adding what should be going on the P and M variable and is adding it on to the W variable.
    I can't see how this is possible from the snippet provided.

    You might be better to post a desensitised workbook with the relevant code included.
    so that we can better understand your aims.

  3. #3
    Registered User
    Join Date
    03-16-2010
    Location
    Chesterfield, England
    MS-Off Ver
    Excel 2016
    Posts
    19

    Re: If statement not adding up correctly

    Using CASE seemed to add everything to the P and PP variables only. I have attached the workbook.

    Thanks.
    Attached Files Attached Files

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: If statement not adding up correctly

    At first sight it seems your problem is how you have defined the variable LOPNum
    It should be
    Dim LOPNum As Double 'NOT as integer

    There is also a fair number of redundant lines in your code.
    I'll go through your code and do a bit of tidying and trimming.
    Look back this afternoon I will have something for you.

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: If statement not adding up correctly

    Try this

    I have
    1/. Added to your code the command to sort sheet "Data" first by Student ID then by Subject. Your code and my revision depend on this.
    The yellow infill on this sheet is for demonstration purposes only, it doesn't affect the code.

    2/. Re-written your code to cut out all unrequired duplicate commands and applied the results directly to their destinations without switching sheets each time.

    3/. Fixed the Select case statement to cover the criteria as best as I can, you may have to alter the criteria to suit conditions I don't know about.


    Switch to Sheet "LoP" and run via your button, no need to sort sheet data, as stated in point #1, the code does this for you.

    Hope this helps
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  6. #6
    Registered User
    Join Date
    03-16-2010
    Location
    Chesterfield, England
    MS-Off Ver
    Excel 2016
    Posts
    19

    Re: If statement not adding up correctly

    That is absolutely fantastic! Thanks very much for all your help.

    I keep promising to teach myself VBA properly one day but its finding the time around all the other work I have to do - so far I’ve just been using a worksheet someone else created and editing the VBA in that.

    Thanks again, you are a star.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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