Sort in descending order

natalie123vba

New Member
Joined
Nov 30, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Web
Hi All, I am trying to sort a column in descending order using the If..then..else method. it worked for me in ascending order but I cant get it to work in descending. any help here would be appreciated. this is the code i used for ascending:

Sub sort_numbersUP()

Dim Sorted As Boolean

Sheets("Sheet1").Range("B5:B25").Copy Destination:=Sheets("Sheet1").Range("C5")

Do
Sorted = False

For numEntries = 5 To 25 - 2

If Cells(numEntries, 3).Value > Cells(numEntries + 1, 3).Value Then
tmpvar = Cells(numEntries + 1, 3)
Cells(numEntries + 1, 3).Value = Cells(numEntries, 3)
Cells(numEntries, 3).Value = tmpvar
Sorted = True

End If

Next

Loop While Sorted

End Sub

My descending column is (F28:F57)

Thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Doesn't change much :

VBA Code:
Sub sort_numbersUP()

Dim Sorted As Boolean

Sheets("Sheet1").Range("B5:B25").Copy Destination:=Sheets("Sheet1").Range("C5")

Do
Sorted = False

For numEntries = 5 To 25 - 2

If Cells(numEntries, 3).Value > Cells(numEntries + 1, 3).Value Then
tmpvar = Cells(numEntries + 1, 3)
Cells(numEntries + 1, 3).Value = Cells(numEntries, 3)
Cells(numEntries, 3).Value = tmpvar
Sorted = True

End If

Next

Loop While Sorted

With Range("F28:F57")
    .Sort Key1:=Range("F28"), _
    Order1:=xlDescending, _
    Header:=xlNo
End With

End Sub
 
Upvote 0
Doesn't change much :

VBA Code:
Sub sort_numbersUP()

Dim Sorted As Boolean

Sheets("Sheet1").Range("B5:B25").Copy Destination:=Sheets("Sheet1").Range("C5")

Do
Sorted = False

For numEntries = 5 To 25 - 2

If Cells(numEntries, 3).Value > Cells(numEntries + 1, 3).Value Then
tmpvar = Cells(numEntries + 1, 3)
Cells(numEntries + 1, 3).Value = Cells(numEntries, 3)
Cells(numEntries, 3).Value = tmpvar
Sorted = True

End If

Next

Loop While Sorted

With Range("F28:F57")
    .Sort Key1:=Range("F28"), _
    Order1:=xlDescending, _
    Header:=xlNo
End With

End Sub
But is there a way of using the same code but changing it to descending? i would have thought that by changing > to < it would work, but no such luck
 
Upvote 0
I remade it in a simplest way :
VBA Code:
Sub sort_numbersUP()

  Dim MyRange1 As Range
  Dim MyRange2 As Range
  
  Sheets("Plan1").Range("B5:B25").Copy Destination:=Sheets("Plan1").Range("C5")

  Set MyRange1 = Sheets("Plan1").Range("C5:C25")

  With MyRange1
    .Sort Key1:=.Columns(1), Order1:=xlDescending, Header:=xlNo
  End With
  
  Set MyRange2 = Sheets("Plan1").Range("F28:F57")
  
    With MyRange2
    .Sort Key1:=.Columns(1), Order1:=xlDescending, Header:=xlNo
  End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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
Back
Top