|
-
Nov 3rd, 2008, 03:39 PM
#1
Thread Starter
New Member
sorting two columns using vba macro
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.
-
Nov 3rd, 2008, 06:19 PM
#2
New Member
Re: sorting two columns using vba macro
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
-
Nov 4th, 2008, 10:09 AM
#3
Thread Starter
New Member
Re: sorting two columns using vba macro
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.
-
Nov 4th, 2008, 10:22 AM
#4
Re: sorting two columns using vba macro
 Originally Posted by sharma1523
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 this
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...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Nov 4th, 2008, 12:53 PM
#5
Thread Starter
New Member
Re: sorting two columns using vba macro
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.
-
Nov 4th, 2008, 12:54 PM
#6
Re: sorting two columns using vba macro
Are there any merged cells in your Workbook?
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Nov 4th, 2008, 12:57 PM
#7
Thread Starter
New Member
Re: sorting two columns using vba macro
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..
-
Nov 4th, 2008, 01:26 PM
#8
Re: sorting two columns using vba macro
 Originally Posted by sharma1523
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Nov 4th, 2008, 01:35 PM
#9
Thread Starter
New Member
Re: sorting two columns using vba macro
Well thanks for all your help..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|