VBA - sort in ascending and descending order (based on if value is positive or negative)

macroos

New Member
Joined
May 30, 2018
Messages
45
Hi All.

I would like to sort my set of data (contains both negative and positive numbers) based in ascending order (if the values are negative) and in descending order (if the values are positive).
How can I do that?
Below, I was able to sort the data on column E in ascending order.
I need help in sorting in descending order if the values are positive.

Sub Step5()


Dim strDataRange As Range
Dim keyRange As Range
Dim lrow As Long
lrow = Cells(Cells.Rows.Count, "B").End(xlUp).Row

Set strDataRange = Range("B2:L" & lrow)
Set keyRange = Range("E1")
strDataRange.Sort Key1:=keyRange, Order1:=xlAscending

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I would like to sort my set of data (contains both negative and positive numbers) based in ascending order (if the values are negative) and in descending order (if the values are positive).
I am not sure what you mean by asscending for negative and descending for positives when you have negatives and positives intermingled. For example, what is the output you want to see for this column of numbers...

4
-6
9
1
2
-4
-1
-7
8
-2
 
Upvote 0
You are wanting to sort the values in Column E starting at Row 2, correct? If so, give this macro a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub SpecialSort()
  Dim X As Long, Data As Variant, Neg As Object, Pos As Object
  Data = Range("E2", Cells(Rows.Count, "E").End(xlUp))
  Set Neg = CreateObject("System.Collections.ArrayList")
  Set Pos = CreateObject("System.Collections.ArrayList")
  For X = 1 To UBound(Data)
    If Data(X, 1) < 0 Then
      Neg.Add Data(X, 1)
    Else
      Pos.Add Data(X, 1)
    End If
  Next
  Pos.Sort
  Pos.Reverse
  Neg.Sort
  Range("E2", Cells(Rows.Count, "E").End(xlUp)).ClearContents
  Range("E2").Resize(Neg.Count) = Application.Transpose(Neg.ToArray)
  Cells(Rows.Count, "E").End(xlUp).Offset(1).Resize(Pos.Count) = Application.Transpose(Pos.ToArray)
  Set Neg = Nothing
  Set Pos = Nothing
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Above, it sorts only the values in E.
If there were data on columns A through D, how can i make it so the whole row will sort based on E?

For example
A B C D E
Jack Jill Neighbor Nice 5
Time Blue Neighbor Mean -2
Apple Good Neighbor Nice 4
Pink Red Neighbor Mean -5

After sorting, it would be this based on E.
A B C D E
Pink Red Neighbor Mean -5
Time Blue Neighbor Mean -2
Jack Jill Neighbor Nice 5
Apple Good Neighbor Nice 4
 
Upvote 0
Above, it sorts only the values in E.
If there were data on columns A through D, how can i make it so the whole row will sort based on E?
Sorry, I misunderstood your layout. Give this macro a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub SpecialSort()
  Dim LastRow As Long, FirstPositive As Long
  LastRow = Cells(Rows.Count, "B").End(xlUp).Row
  ActiveSheet.Sort.SortFields.Clear
  ActiveSheet.Sort.SortFields.Add Key:=Range("E2:E" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  With ActiveSheet.Sort
      .SetRange Range("B2:M" & LastRow)
      .Header = xlNo
      .Orientation = xlTopToBottom
      .Apply
  End With
  FirstPositive = Columns("E").Find("-*", , xlValues, , xlRows, xlPrevious, , , False).Row + 1
  ActiveSheet.Sort.SortFields.Clear
  ActiveSheet.Sort.SortFields.Add Key:=Range("E" & FirstPositive & ":E" & LastRow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
  With ActiveSheet.Sort
    .SetRange Range("B" & FirstPositive & ":M" & LastRow)
    .Header = xlNo
    .Orientation = xlTopToBottom
    .Apply
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
This error came out - Run-time error '91: Object variable or With block variable not set

It looks like there's an issue with this line:
FirstPositive = Columns("E").Find("-*", , xlValues, , xlRows, xlPrevious, , , False).Row + 1
 
Upvote 0
This error came out - Run-time error '91: Object variable or With block variable not set

It looks like there's an issue with this line:
FirstPositive = Columns("E").Find("-*", , xlValues, , xlRows, xlPrevious, , , False).Row + 1
You have numbers in Column E, correct?

How are the numbers getting into the cell?

Is it possible for you to have no negative values in Column E?

Is it possible for you to have no positive values in Column E?
 
Last edited:
Upvote 0
Yes, there are positive and negative numbers in Column E.

When I clicked the macro, it sorted all in ascending order and then the error pops up.
Only the negative numbers were sorted the way I wanted.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,896
Members
449,194
Latest member
JayEggleton

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