I have a excel having a to z columns.I want to write a macro for sorting column b and column d in the ascending order irrespective of the number of rows.And I would appreciate early help.
Printable View
I have a excel having a to z columns.I want to write a macro for sorting column b and column d in the ascending order irrespective of the number of rows.And I would appreciate early help.
quick and nasty with the macro recorder, but try this;
Code:Columns("A:A").Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Columns("B:B").Select
Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
this one is fine.But I need something dynamic as i have lot of excels like this.I need a generic code which will sort columns b and column d so that i can paste the code in all the excels for sorting.
Try thisQuote:
Originally Posted by sharma1523
vb Code:
Sub SortIfThereAreHeaders() 'Sorts taking headers into account Columns("A:Z").Sort Key1:=Range("B2"), Order1:=xlDescending, Key2:=Range("D2") _ , Order2:=xlDescending, Header:=xlYes, OrderCustom:=1, MatchCase:=False _ , Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:= _ xlSortNormal End Sub Sub SortIfThereAreNoHeaders() 'Sorts without taking headers into account Columns("A:Z").Sort Key1:=Range("B1"), Order1:=xlDescending, Key2:=Range("D1") _ , Order2:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _ False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _ :=xlSortNormal End Sub
Rest of the code depends on how are you opening excel workbooks...
Code is fine.But when I m running it it gives me error "this operation requires the merged cells to be identically sized".Is there any code with variables which deosn't give this error.I really appreciate ur time and efforts.
Are there any merged cells in your Workbook?
Yes there are few on the top of the sheet.Is there a way to do without changing those...i mean can we do some cell(1,2) like that..
No we can't :)Quote:
Originally Posted by sharma1523
Well thanks for all your help..