+ Reply to Thread
Results 1 to 10 of 10

Adapting Bubblesort to work with Long datatype array

  1. #1
    Registered User
    Join Date
    09-21-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    71

    Adapting Bubblesort to work with Long datatype array

    I've tried about every combination I could think of but no dice...

    This is the original code coming from Microsoft's website and out of the box it works. So I thought, let's try to make it work for me.

    Please Login or Register  to view this content.

    This I what I changed it into to adapt to my needs:
    Please Login or Register  to view this content.
    That get's me the Error 'Byref argument types do not match'.

    So I tried calling it like this:

    Please Login or Register  to view this content.
    Which gets me the Error 'Matrix argument must be by ref. '
    Also what goes beyond me is how True can be assigned to the NoExchanges integer instead of a boolean. I do understand how bubblesort works, I just can get it to sort my array. Sorry for being such a noob...
    Last edited by Jeroen1000; 09-23-2009 at 10:16 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Adapting Bubblesort to work with Long datatype array

    As you've rewritten the code, it takes a single variable (a Long), which doesn't leave much to sort. Change it back to a Variant.

    I didn't look past the first line.

    A bubble sort is about the least efficient sort possible, BTW. You wouldn't want to sort more than a few hundred itemes, or even fewer if you're doing it a lot.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-21-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    71

    Re: Adapting Bubblesort to work with Long datatype array

    I don't quite follow VBA logic in that case.

    The original code also passes a single variable (one array), but as a Variant.
    When I make the Long array in my code it holds 3 values. The rest is empty.
    The orignal codes also defines an array and puts values into it.

    It looks to me I did the same with longs instead of variants?

    Please Login or Register  to view this content.
    As I understand, a Variant can hold every datatype. As I only work with 'Long' values I assumed I did not need a Variant array.

    I have written a function before and it had to be declared this way
    Please Login or Register  to view this content.
    where as Microsoft's Function does not seem to have a return data type.
    Very confusing to newbies. Still reading up on the matter though.
    Last edited by Jeroen1000; 09-23-2009 at 09:29 AM.

  4. #4
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Adapting Bubblesort to work with Long datatype array

    This works for me

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-21-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    71

    Re: Adapting Bubblesort to work with Long datatype array

    Thanks Mr. Philips. I've got it running too now. Peculiar how the function is being called now.

    Please Login or Register  to view this content.
    is not allowed

    I should really go for a tutorial. Too many differences with Java for me.

    As the 'original' array has been altered, is passing by reference the default behavior then?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Adapting Bubblesort to work with Long datatype array

    Arrays are always passed by reference in VBA.

    A Variant can hold any kind of data, including arrays. As the argument to a function, a Variant can receive any type of data passed; but a Variant passed to a function can only be received as a Variant; does that make sense?

    When a VBA function does not have an explicitly declared return type, it is, by default, a Variant, just like declaring a variable without specifying a type.
    Last edited by shg; 09-23-2009 at 10:27 AM.

  7. #7
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Adapting Bubblesort to work with Long datatype array

    Quote Originally Posted by Jeroen1000 View Post
    T Peculiar how the function is being called now.

    Please Login or Register  to view this content.
    is not allowed
    When you enclose the parameter in brackets, VBA tries to evaualte the expression, and it can't with the array, hence the error.

  8. #8
    Forum Contributor
    Join Date
    09-22-2009
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Office 2003 / Office 2007
    Posts
    114

    Re: Adapting Bubblesort to work with Long datatype array

    Worth pointing out that this is typically a type of option that can be "cheated".

    If the array is in VBA (created by running code etc) then transpose the array directly to the worksheet:

    Please Login or Register  to view this content.
    Once the values you need to sort are on a sheet, use the built in sort function. This is typically vastly faster than actually attempting to do it with VBA.

  9. #9
    Registered User
    Join Date
    09-21-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    71

    Re: Adapting Bubblesort to work with Long datatype array

    Most interesting explanations. And yes, it does make sense. I learned what what reference and by value was during Java classes

    Also thanks for pointing out what happens with the brackets. I've made much progress incorperation my code and using the 'record macro' cheating for things I don't know.

    It's astounding how much 'crappy code' Excel generates when you record the macro!

  10. #10
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Adapting Bubblesort to work with Long datatype array

    Quote Originally Posted by Jeroen1000 View Post
    Most interesting explanations. And yes, it does make sense. I learned what what reference and by value was during Java classes

    Also thanks for pointing out what happens with the brackets. I've made much progress incorperation my code and using the 'record macro' cheating for things I don't know.

    It's astounding how much 'crappy code' Excel generates when you record the macro!
    It is not so surprising really. Most code generators (seem to) generate far more than is needed. It takes a simplistic view of what you did rather than trying to second guess what you are doing, and caters for multiple scenarios. It does what it needs to do, shows you the object model.

+ 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