Switch sort direction each time macro is invoked

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • halooti
    New Member
    • Feb 2008
    • 3

    Switch sort direction each time macro is invoked

    Hello all

    I created a button with excel and assigned a macro to it which I recorded with excel Macro Record..

    The button sorts column C ascending, this is the code

    [CODE=vb]Sub Macro1()
    Range("C7:C11") .Select
    Selection.Sort Key1:=Range("C7 "), Order1:=xlAscen ding, Header:=xlGuess ,
    OrderCustom:=1, MatchCase:=Fals e, Orientation:=xl TopToBottom, _
    DataOption1:=xl SortNormal
    Range("D9").Sel ect
    End Sub[/CODE]

    I want to make this macro sorts Descending when I click on the button again ..
    and the Ascending with another click and so on ..

    If anyone knows how to do it .. please write the code ..

    thanks
    Last edited by Killer42; Feb 13 '08, 02:02 AM.
  • ubentook
    New Member
    • Dec 2007
    • 58

    #2
    [CODE=vb]Sub Macro1_R1()
    Dim lngWay As Long
    If Range("C11").Va lue > Range("C7").Val ue Then
    lngWay = xlDescending
    Else
    lngWay = xlAscending
    End If
    Range("C7:C11") .Sort Key1:=Range("C7 "), Order1:=lngWay, _
    Header:=xlGuess , OrderCustom:=1, MatchCase:=Fals e, _
    Orientation:=xl TopToBottom, DataOption1:=xl SortNormal
    Range("D9").Sel ect
    End Sub[/CODE]


    Originally posted by halooti
    Hello all

    I created a button with excel and assigned a macro to it which I recorded with excel Macro Record..

    The button sorts column C ascending, this is the code

    Sub Macro1()
    Range("C7:C11") .Select
    Selection.Sort Key1:=Range("C7 "), Order1:=xlAscen ding, Header:=xlGuess ,
    OrderCustom:=1, MatchCase:=Fals e, Orientation:=xl TopToBottom, _
    DataOption1:=xl SortNormal
    Range("D9").Sel ect
    End Sub


    I want to make this macro sorts Descending when I click on the button again ..
    and the Ascending with another click and so on ..

    If anyone knows how to do it .. please write the code ..

    thanks
    Last edited by Killer42; Feb 13 '08, 02:03 AM.

    Comment

    • halooti
      New Member
      • Feb 2008
      • 3

      #3
      Originally posted by ubentook
      Sub Macro1_R1()
      Dim lngWay As Long
      If Range("C11").Va lue > Range("C7").Val ue Then
      lngWay = xlDescending
      Else
      lngWay = xlAscending
      End If
      Range("C7:C11") .Sort Key1:=Range("C7 "), Order1:=lngWay, _
      Header:=xlGuess , OrderCustom:=1, MatchCase:=Fals e, _
      Orientation:=xl TopToBottom, DataOption1:=xl SortNormal
      Range("D9").Sel ect
      End Sub

      THANKS UBENTOOK !!!!

      Comment

      • halooti
        New Member
        • Feb 2008
        • 3

        #4
        I want to use this sub with a different column and i want to know what's the function of this so that i can change it to suit the new table :

        Range("D9").Sel ect

        Comment

        • kadghar
          Recognized Expert Top Contributor
          • Apr 2007
          • 1302

          #5
          Originally posted by halooti
          I want to use this sub with a different column and i want to know what's the function of this so that i can change it to suit the new table :

          Range("D9").Sel ect
          Use WORKBOOKS(index ), WORKSHEETS(inde x) and CELLS(row, col), if you dont specify one of the first two, it'll use the active one.

          e.g.

          workbooks(1).wo rksheets(2).cel ls(9,4) will refer to the cell D9.

          You can put them into a With to make things easier, and use ranges with cells:

          [CODE=vb]with workbooks(1).wo rksheets(2)
          range(.cells(9, 4), .cells(15,5)) = "hi"
          end with[/CODE]
          or use the name of the book/sheet/range, instead of the index number:

          workbooks("mybo ok").worksheets ("sheet1").rang e("B9:F17")

          and this names are strings, so you can modify them as any other string.

          HTH
          Last edited by kadghar; Feb 11 '08, 05:27 PM. Reason: add code tags

          Comment

          • ubentook
            New Member
            • Dec 2007
            • 58

            #6
            Range("D9").Sel ect is not required; you can delete it.
            '--
            The code sorts in Column C.
            To have the code work on a different column then...
            where one or more upper case C's follows the word Range, change C to the letter of the new column.
            '--


            Originally posted by halooti
            I want to use this sub with a different column and i want to know what's the function of this so that i can change it to suit the new table :

            Range("D9").Sel ect

            Comment

            Working...