How to Sort a 2 Dimensional Array on two Columns in Excel VBA

Sorting a two dimensional array in Excel VBA based on two columns, can be completed fairly easily using the code below.

The code assumes that your array starts at 0. So, in other words you have not declared Option Base = 1.

Where it says Sort1=1 this is referring to the array and specifying that you wish to sort the array by the second column which is 1.

Where it says Sort2=7 this is referring to the array and specifying that you wish to sort the array by the eighth column, which is 7.

Remember, in Excel arrays the rows and columns start at 0, unless at the very top of the code you write Option Base 1. Having this at the top will force an array to start at 1 and not 0. So that when you reference an array element, you call it by its actual location, for example (1,2). This could be (row 1, column 2) depending on how your array is set up. Whereas, without the Option Base 1 at the top you would reference the previous element as (0,1).

When you see > this is what specifys the ascending sort. To sort descending simply swap > with <. For example:

Condition1= YourArrayName (Y, Sort1) > YourArrayName (Y + 1, Sort1) – Is ascending

Condition1= YourArrayName (Y, Sort1) < YourArrayName (Y + 1, Sort1) – Is descending

The LBound(YourArrayName,1) specifies the lowest possible value in the array. In this case it would be zero.

The UBound(YourArrayName,1) specifies the highest possible value in the array. So this would be the number of the last element in the array.

Sort1=1
Sort2=7
For X = LBound(YourArrayName, 1) To UBound(YourArrayName, 1) - 1
For Y = LBound(YourArrayName, 1) To UBound(YourArrayName, 1) - 1
Condition1= YourArrayName (Y, Sort1) > YourArrayName (Y + 1, Sort1)
Condition2= YourArrayName (Y, Sort1) = YourArrayName (Y + 1, Sort1) And _
YourArrayName (Y, Sort2) > YourArrayName (Y + 1, Sort2)

If Condition1 or Condition2 then
For Z = LBound(YourArrayName, 2) To UBound(YourArrayName, 2)
t = YourArrayName (j, y)
YourArrayName (Y, Z) = YourArrayName (Y + 1, Z)
YourArrayName (Y + 1, Z) = t
Next Z
End If
Next Y
Next X

I hope you found this post useful!

4,331 thoughts on “How to Sort a 2 Dimensional Array on two Columns in Excel VBA

  1. Poker Online Terpercaya

    I’ve been exploring for a little bit for any high quality articles or weblog posts on this kind of area .
    Exploring in Yahoo I ultimately stumbled upon this website.
    Studying this info So i am glad to show that I have
    a very excellent uncanny feeling I found out just what I
    needed. I such a lot indubitably will make certain to don?t
    forget this site and provides it a glance regularly.

  2. HP driver

    Next time I read a blog, Hopefully it won’t disappoint me just as much as this one. After all, Yes, it was my choice to read, nonetheless I truly thought you would probably have something useful to talk about. All I hear is a bunch of crying about something that you can fix if you were not too busy searching for attention.

  3. udfauzi.com

    Hello there, I do think your blog could be having browser compatibility problems. When I take a look at your web site in Safari, it looks fine however, if opening in Internet Explorer, it’s got some overlapping issues. I merely wanted to provide you with a quick heads up! Aside from that, fantastic site!

  4. harga-jual

    I truly love your blog.. Excellent colors & theme. Did you make this web site yourself? Please reply back as I’m hoping to create my very own blog and want to learn where you got this from or just what the theme is called. Cheers!

  5. Agen Poker Terpercaya

    Heya i’m for the primary time here. I came across this board and I in finding It truly helpful & it helped me out much. I’m hoping to provide something again and aid others like you aided me.|

  6. https://iamgutsclub.com/

    I’m impressed, I have to admit. Rarely do I come across a blog that’s both equally educative and interesting, and let me tell you, you’ve hit the nail on the head. The issue is something that not enough people are speaking intelligently about. I am very happy I found this in my search for something regarding this.

  7. hentaisea

    Very nice article and straight to the point. I am not sure if this is actually the best place to ask but do you people have any thoughts on where to hire some professional writers? Thanks in advance

  8. slot online

    Having read this I thought it was rather enlightening. I appreciate you finding the time and energy to put this informative article together. I once again find myself spending way too much time both reading and commenting. But so what, it was still worthwhile!

  9. vivoslot

    After going over a number of the blog posts on your web page, I honestly appreciate your way of blogging. I saved it to my bookmark webpage list and will be checking back in the near future. Please visit my web site as well and let me know how you feel.

  10. apk vivoslot

    Nice post. I learn something totally new and challenging on blogs I stumbleupon on a daily basis. It’s always exciting to read through content from other authors and use a little something from other websites.

  11. download vivoslot apk

    May I just say what a comfort to uncover somebody that actually knows what they’re discussing on the net. You actually understand how to bring an issue to light and make it important. More and more people should read this and understand this side of your story. I was surprised that you’re not more popular because you certainly possess the gift.

  12. vivo slot

    You’re so interesting! I do not suppose I’ve read through something like this before. So great to discover someone with some original thoughts on this topic. Really.. many thanks for starting this up. This web site is one thing that is required on the internet, someone with some originality!

  13. link alternatif vivoslot

    Hi! I could have sworn I’ve visited your blog before but after looking at a few of the articles I realized it’s new to me. Nonetheless, I’m certainly pleased I found it and I’ll be book-marking it and checking back frequently!

  14. togel online

    Next time I read a blog, Hopefully it doesn’t fail me as much as this one. I mean, Yes, it was my choice to read, however I genuinely thought you would probably have something helpful to say. All I hear is a bunch of whining about something that you can fix if you weren’t too busy looking for attention.

  15. vivoslot mobile

    I absolutely love your site.. Excellent colors & theme. Did you make this site yourself? Please reply back as I’m attempting to create my own website and want to learn where you got this from or exactly what the theme is called. Cheers!

  16. 谷歌SEO

    What’s up to every one, the contents present at this site are
    in fact amazing for people experience, well,
    keep up the good work fellows.

Comments are closed.