+ Reply to Thread
Results 1 to 14 of 14

Middle number function

  1. #1
    Registered User
    Join Date
    09-16-2010
    Location
    AZ, USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Middle number function

    I have an assignment for my engineering class in which I must write a VBA program that finds the middle number of three numbers. My program compiles, but it returns 0 each time. I am not sure what I am doing wrong. Here is the code:
    Please Login or Register  to view this content.
    Last edited by romperstomper; 09-16-2010 at 09:38 AM. Reason: add code tags

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,276

    Re: Middle number function

    You have your assignments to x the wrong way round (you are assigning x to and b, not vice versa)

    Note: when posting code you must use code tags. I have added them for you as it's your first post, but please remember to use them in future.
    Remember what the dormouse said
    Feed your head

  3. #3
    Registered User
    Join Date
    09-16-2010
    Location
    AZ, USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Middle number function

    That fixed the first issue, now there's a logical error. It does not always display the middle number.

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Middle number function

    There's more to it than that.

    Take the case a=11, b=10, c=9

    a>b so x=11

    x < c is false, so y=9

    Hence the function returns 9 as the middle value, despite it being the lowest.

  5. #5
    Registered User
    Join Date
    09-16-2010
    Location
    AZ, USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Middle number function

    Nevermind, I figured it out. Thanks for your help though!

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,276

    Re: Middle number function

    Can you not just use Median?
    Since it's your assignment, I can't assist you with the theory, just any code errors.

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Middle number function

    Please Login or Register  to view this content.



  8. #8
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Middle number function

    And if you've done that then why not ...

    Please Login or Register  to view this content.
    Last edited by Andrew-R; 09-16-2010 at 10:03 AM. Reason: Simplifying

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,276

    Re: Middle number function

    Or use median...

  10. #10
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Middle number function

    Where's your sense of adventure?

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Middle number function

    Please Login or Register  to view this content.

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,276

    Re: Middle number function

    If I wanted adventure, I'd bubble sort the array and then grab the middle one.

  13. #13
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Middle number function

    What if there are an even number of numbers? Does median work then?

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,276

    Re: Middle number function

    It returns the average of the middle two.

+ 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