+ Reply to Thread
Results 1 to 8 of 8

VBA produces wrong output for loops

  1. #1
    Registered User
    Join Date
    10-12-2014
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    54

    VBA produces wrong output for loops

    Hi all,

    I have the code shown below. The output of this code generates is incorrect as every cell defined in the 'in1' column is set to 'INPUT RATE'. I believe the '<>' means "does not equal" and thought the values in the 'in1' column that are not the same in the 'inn' column would output 'INPUT RATE" but it also does this to the ones that do equal as well. If someone can point me in the right direction that would be appreciated.

    Cheers.

    Please Login or Register  to view this content.

  2. #2
    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
    44,364

    Re: VBA produces wrong output for loops

    VBA doesn't produce the wrong output, your logic/code does. If you have A, B and C in W5:W7 and A in F9, first time through the inner loop, they are equal. After that, they're not, hence you get INPUT RATE. If you compare any cell to three things, it's always going to not be equal to one of them.

    Use COUNTIF to check if the cell is present in the small range.

    Regards, TMS
    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


  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA produces wrong output for loops

    Hi,

    It's a little difficult to comment with certainty without seeing the problem in the context of the workbook.

    Have you tried stepping through the code up to the IF line in question and then hovering over the in1.value and then the inn.value to identify what they are. (Or add a Debug.Print in1.value and Debug.Print inn.value immediately before so that the Immediate window will record them.

    That may help you discover the problem.

    You might also usefully use the WATCH window and set a break for in1<>inn and then in1=inn so that you can see when inconsistencies occur.

    Otherwise upload the workbook.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    10-12-2014
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    54

    Re: VBA produces wrong output for loops

    Thanks for the response guys, I'm not familiar with countif in vba so I'm not touching that yet. Also I have a mockup of the excel file so you can see what I mean. Basically if I run the code the excel outputs all "INPUT RATE", I basically want the one with 80 to have "INPUT RATE" and the others have the appropriate rate. I've also put some more code in and it seemed to improve some of the output but its not working as like it to work.

    Please have a look on the excel file.

    example.xlsm

    Cheers

  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
    44,364

    Re: VBA produces wrong output for loops

    Untested:

    Please Login or Register  to view this content.


    Regards, TMS

  6. #6
    Registered User
    Join Date
    10-12-2014
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    54

    Re: VBA produces wrong output for loops

    @ TMS,

    You are a legend!!

    This has been racking my brain for quite some time. I see that you put an "Else" command and then followed it with an underscore. What sort of procedure is this? I'm still learning VBA myself so I want to have a brief breakdown on whats going on.

    Cheers

  7. #7
    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
    44,364

    Re: VBA produces wrong output for loops

    You're welcome

    The underscore isn't anything to do with the If/Else/End If. You can use an underscore to split a line of code over two or more lines. It's mainly to make the code more readable.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  8. #8
    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
    44,364

    Re: VBA produces wrong output for loops

    Thanks for the rep.


    Fully commented version with variables defined:

    Please Login or Register  to view this content.

    Regards, TMS

+ 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. What is wrong with this code? Loops...
    By esbenhaugaard in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2012, 12:50 PM
  2. Getting false when i should be getting true in (=INT(CELL)=CELL)
    By phbryan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-28-2012, 09:51 AM
  3. Wrong Output after first run
    By vijay2482 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-16-2009, 04:20 AM
  4. [SOLVED] Fill down produces correct formula but wrong answer
    By Jim at SDSU in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-03-2006, 03:10 PM
  5. FORMULA PRODUCES WRONG RESULT
    By Wildebeest222 in forum Excel General
    Replies: 2
    Last Post: 10-11-2005, 05:05 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