[RESOLVED] Find the closest Number compare to a list of Numbers-VBForums
Results 1 to 9 of 9

Thread: [RESOLVED] Find the closest Number compare to a list of Numbers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    139

    Resolved [RESOLVED] Find the closest Number compare to a list of Numbers

    Hi,Iam hoping someone can help me with this.
    I have a flexgrid that contians 5 numbers. And I want to be abel to compare each number of the flexgrid to an array of 15 values (0 to 14) if the value in the flexgrid is closest to the value in the array then I want to show both values (flexgrid value and array closest value) int a textbox with the Multiline in true. any help would be apreciated.

  2. #2
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Find the closest Number compare to a list of Numbers

    Easiest way would be to sort the list of values and then look through them until you find your value is lower than the next one then compare the difference between the current and the next against the value you're working with and you can work out which one is closest.
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  3. #3
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    11,999

    Re: Find the closest Number compare to a list of Numbers

    Quote Originally Posted by Monti124 View Post
    I have a flexgrid that contians 5 numbers. And I want to be abel to compare each number of the flexgrid to an array of 15 values (0 to 14) if the value in the flexgrid is closest to the value in the array then I want to show both values (flexgrid value and array closest value) int a textbox with the Multiline in true
    Showing us some real data may help. Show us the 5 numbers and the 15 values.

    What if 1 or more of the 5 match 1 or more of the 15?
    If 1 of the 5 matches 1 of the 15 exactly, do you stop or continue on?
    Or is this something like: for each of the 5, find its closest match in the 15?

    You see, I'm a bit confused too, and if I am, others may be too. Better details could be helpful for us.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum. Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    139

    Re: Find the closest Number compare to a list of Numbers

    thank you both for the response and to answer the question . Here are part of the numbers I want ot compare.
    in My flexgrid I have the following numbers.

    .1980
    .1990
    .2000
    .2010
    .2020

    and I want to compare each of these value to a set of predefine values. Like.

    0.1992
    0.1996
    0.2000
    0.2004
    0.2008
    0.2012
    0.2016

    for example: if 1992 is the closest number to 1980 then a Want to put 1192 in a text box.or put any number I choose.
    hope these explanation works.

  5. #5
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    11,999

    Re: Find the closest Number compare to a list of Numbers

    Is this for each of the 5 numbers?
    One more question. What if the difference is identical both below and above? Example
    Test 5 against 3, 7, 9, 13, 21, etc. The difference btwn 5 & 3 and 5 & 7 are the same.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum. Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    139

    Re: Find the closest Number compare to a list of Numbers

    in your ecenario I would prefere 7 an 3 as the answer.
    since the two numbers are closest to 5.
    in my program this is what Iam trying to
    acomplish.
    Last edited by Monti124; Feb 18th, 2010 at 10:14 PM.

  7. #7
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    11,999

    Re: Find the closest Number compare to a list of Numbers

    Well, here's what I suggest, especially if your array of 15 is sorted. Psuedo code follows:
    Code:
    For Nr = 1 To 5
        lBestMatchIndex = 0
        For X = 1 To UBound(arrayOf15)
           If Abs(flexGridNr(Nr) - arrayOf15(X)) < Abs(flexGridNr(Nr) - arrayOf15(lBestMatchIndex)) Then
               lBestMatchIndex = X
           End If
        Next
        ' now you have the best match, you can compare it to the one above to see if they are same difference
        If lBestMatchIndex = UBound(arrayOf15) Then ' no comparison needed
            Debug.Print "Best Match for "; Nr; " is "; arrayOf15(lBestMatchIndex)
        ElseIf Abs(flexGridNr(Nr) - arrayOf15(lBestMatchIndex)) = Abs(flexGridNr(Nr) - arrayOf15(lBestMatchIndex + 1)) Then
             ' number high & low have same difference
            Debug.Print "Best Match for "; Nr; " is "; arrayOf15(lBestMatchIndex ); " and "; arrayOf15(lBestMatchIndex + 1)
        Else
            Debug.Print "Best Match for "; Nr; " is "; arrayOf15(lBestMatchIndex)
        End IF
    Next
    Edited: fixed some typos with lBestMatchIndex. Anyway, I think you can follow the logic; and that's the main purpose of pseudo code
    Last edited by LaVolpe; Feb 19th, 2010 at 08:20 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum. Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    139

    Re: Find the closest Number compare to a list of Numbers

    Thank you LaVolpe, I just plug your code into my program an it worked perfect, thats exactly What I wanted My program to do. Thanks for your time

  9. #9
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    11,999

    Re: [RESOLVED] Find the closest Number compare to a list of Numbers

    You are welcome. Keep in mind that if your arrayOf15 is not sorted, that ELSEIF statement will not be a valid way to determine if more than one number is closest. If that is a big requirement, you will want to ensure your array is sorted.

    Edited: You may want to cache this portion of the calcs inside the loop when a new bestmatch is found. No need to recalculate it every time
    Code:
    Abs(flexGridNr(Nr) - arrayOf15(lBestMatchIndex))
    Last edited by LaVolpe; Feb 19th, 2010 at 08:20 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum. Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.