VBA Multiple Column Sort

scvba

New Member
Joined
Mar 20, 2009
Messages
8
Hi all,

Hope you can help, I am trying to sort data in multiple columns for a specfic range within a sheet, this code almost looks like it wants to work but I keep getting mismatch error. I want to sort only within the range "B4:ET404" and there is no header...can someone please help me with the right coding?? I would appreciate it very much! :)


Sub MultiSort()
Dim Cols As Range, i As Integer
Set Cols = Selection
For i = Range("B:ET") To Cols.Columns.Count
Cols.Columns(i).Sort Key1:=Cols.Columns(i).Cells(4, 404), _
Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Next i
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

DavidB

Active Member
Joined
Oct 21, 2002
Messages
253
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi there - welcome to the board.

You immediate problem revolves around "Range("B:ET")" not returning a numeric value for the for statement.

Sorry I have not got my head around the rest of your logic but this should get you moving in the right direction.

Regards DavidB
 
Upvote 0

DavidB

Active Member
Joined
Oct 21, 2002
Messages
253
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi there


The routine sorts each column one at a time and independently of all the others.

Sub MultiSort1()
Dim Cols As Range, i As Integer
Set Cols = Range("B2:ET404")
For i = 0 To Cols.Columns.Count
Cols.Columns(i).Sort Key1:=Cols.Columns(i), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Next i
End Sub


Is this is what you want?

Regards David
 
Upvote 0
L

Legacy 68668

Guest
change

Cols.Columns(i).Cells(4, 404)

to

Cols.Columns(i).Cells(1)

If you have heading, change to 2
 
Upvote 0

scvba

New Member
Joined
Mar 20, 2009
Messages
8
david,

that was perfect, thank you so much....
cheers,
--==sandra

Hi there


The routine sorts each column one at a time and independently of all the others.

Sub MultiSort1()
Dim Cols As Range, i As Integer
Set Cols = Range("B2:ET404")
For i = 0 To Cols.Columns.Count
Cols.Columns(i).Sort Key1:=Cols.Columns(i), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Next i
End Sub


Is this is what you want?

Regards David
 
Upvote 0

MeghaJ

Board Regular
Joined
Jul 2, 2009
Messages
102
David i wanna use this code. but for me.. rows numbers are not known.. they are dynamic.. then how to define range.? plz help
 
Upvote 0

DavidB

Active Member
Joined
Oct 21, 2002
Messages
253
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi there MeghaJ

There are a number of ways of doing this:
http://www.mrexcel.com/forum/showthread.php?t=67618
The current region is an area bounded by empty rows/coulmns. This is equivalent to edit/goto/special and selecting current region.

Another way is to use UDF (User Defined Function) code to determine the last row and then use a string concatenation to set the row limit.

Public Function FinalRow()
FinalRow = Range("a65536").End(xlUp).Row
End Function

This code is written for Excel 2003 where the maximum row number is 65536. If you are using Excel 2007 the number is bigger.

Another more risky way is to think of the maximum number of rows that you will ever encounter. If you exceed this limit then only some of the database will be sort.

Regards DavidB
 
Upvote 0

Kim Eichman

New Member
Joined
Aug 14, 2011
Messages
2
I am using Excel 2007. How would the code below change if I want to sort a range of cells based on the values in a single column in that range.

Hi there


The routine sorts each column one at a time and independently of all the others.

Sub MultiSort1()
Dim Cols As Range, i As Integer
Set Cols = Range("B2:ET404")
For i = 0 To Cols.Columns.Count
Cols.Columns(i).Sort Key1:=Cols.Columns(i), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Next i
End Sub


Is this is what you want?

Regards David
 
Upvote 0

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
I am using Excel 2007. How would the code below change if I want to sort a range of cells based on the values in a single column in that range.

Try like this

Code:
Sub MultiSort1()
Range("B2:ET404").Sort Key1:=Range("C2"), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub
 
Upvote 0

Kim Eichman

New Member
Joined
Aug 14, 2011
Messages
2
OK, I currently have the following VBA code:
Sub MultiSort1()
Range("Promethee_II!B2:D15").Sort Key1:=Range("Promethee_II!D2:D15"), Order1:=xlDescending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub

I am trying to sort a range in the tab called Promethee_II from a tab called User Interface. When I hit a Macro activated button on the User Interface tab, nothing seems to happen.

When I hit the run button in the VBA editor it seems to run (ie. no errors) but nothing gets sorted. Excuse my ignorance but any help you can give will be appreciated.

Thank you.

Try like this

Code:
Sub MultiSort1()
Range("B2:ET404").Sort Key1:=Range("C2"), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,517
Messages
5,987,057
Members
440,074
Latest member
Emmanuelian

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top