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!
Very good article.Much thanks again. Cool.
Im grateful for the post.Really thank you! Really Great.
This is a topic which is near to my heart… Take care! Where are your contact details though?
Ponto IPTV a melhor programacao de canais IPTV do Brasil, filmes, series, futebol, lutas, shows, documentarios em alta definicao de imagem e som em SD, HD, FULL HD, 4K. compativel com todos aparelhos do mercado Brasileiro.
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.
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.
Wow, wonderful blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your site is
great, as well as the content!
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!
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!
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.|
I love looking through an article that can make men and women think. Also, thank you for allowing me to comment!
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.
I truly appreciate this blog. Want more.
This piece of writing is actually a pleasant one it assists new the web viewers, who are wishing for blogging.|
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
Ponto IPTV a melhor programacao de canais IPTV do Brasil, filmes, series, futebol, lutas, shows, documentarios em alta definicao de imagem e som em SD, HD, FULL HD, 4K. compativel com todos aparelhos do mercado Brasileiro.
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!
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.
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.
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.
There’s certainly a great deal to know about this topic. I love all the points you’ve made.
I really liked your blog article.Thanks Again. Fantastic.
Way cool! Some very valid points! I appreciate you writing this write-up and the rest of the website is very good.
Way cool! Some extremely valid points! I appreciate you writing this post and also the rest of the site is also really good.
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!
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!
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.
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!
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.
Hі therе, aⅼl the time i used to check
webpage posts here early in the morning, becaᥙse i love to find
out m᧐re and more.
This web site truly has all of the information and facts I needed about this subject and didn’t know who to ask.