Results 1 to 9 of 9

Thread: sorting two columns using vba macro

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    8

    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.

  2. #2
    New Member
    Join Date
    Nov 2008
    Posts
    3

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    8

    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.

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: sorting two columns using vba macro

    Quote 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:
    1. Sub SortIfThereAreHeaders()
    2.     'Sorts taking headers into account
    3.     Columns("A:Z").Sort Key1:=Range("B2"), Order1:=xlDescending, Key2:=Range("D2") _
    4.     , Order2:=xlDescending, Header:=xlYes, OrderCustom:=1, MatchCase:=False _
    5.     , Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:= _
    6.     xlSortNormal
    7. End Sub
    8.  
    9. Sub SortIfThereAreNoHeaders()
    10.     'Sorts without taking headers into account
    11.     Columns("A:Z").Sort Key1:=Range("B1"), Order1:=xlDescending, Key2:=Range("D1") _
    12.     , Order2:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
    13.     False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
    14.     :=xlSortNormal
    15. 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

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    8

    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.

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    8

    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..

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: sorting two columns using vba macro

    Quote 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

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    8

    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
  •  



Click Here to Expand Forum to Full Width