Sort (Z-A) Macro not working

gomes123

New Member
Joined
Jun 16, 2021
Messages
35
Office Version
  1. 2007
Platform
  1. Windows
I've got a macro below, row 2 is the heading (in a table), and I'd like to (in priority) filter I2:
select the criteria UP, DOWN, LEFT, RIGHT,
sort Z-A (descending).
Then filter J2:
sort Largest to smallest

The macro is able to filter UP, DOWN, LEFT, RIGHT, but doesn't seem to sort from Z-A.
Any ideas why? Thanks!

VBA Code:
Sub Breakthrough()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Main")
 
    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
 

    ws.Range("I3").Formula = "=IF(AND(E3>C3,G3>E3,F3>D3,H3>F3),""UP"",IF(AND(C3>E3,E3>G3,D3>F3,F3>H3),""DOWN"",IF(AND(E3>C3,G3>E3,D3>F3,F3>H3),""RIGHT"",IF(AND(C3>E3,E3>G3,F3>D3,H3>F3),""LEFT"",""NO""))))"

    Range("C3:L" & Range("A" & Rows.Count).End(xlUp).Row).FillDown
 
    Range("I2").Select
    Dim LastRow2 As Long
LastRow2 = Range("A" & Rows.Count).End(xlUp).Row
Selection.AutoFilter
ActiveSheet.Range("$A$2:$L$" & LastRow2).AutoFilter Field:=9, Criteria1:=Array( _
        "DOWN", "LEFT", "RIGHT", "UP"), Operator:=xlFilterValues
ActiveWorkbook.Worksheets("Main").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Main").AutoFilter.Sort.SortFields.Add Key:=Range( _
"J2:J" & LastRow2), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Main").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

 
End Sub
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
The FillDown caused me issues with your macro. It simply copied the top row all the way down throught the range. Other than that it seemed to work. I commented out the following statement and then it worked. If you need this for a reason, step through and make sure this step is doing what you want it to do.
VBA Code:
Range("C3:L" & Range("A" & Rows.Count).End(xlUp).Row).FillDown

Here is the random data I created for testing. I had column A sorted numerically before running the macro. I tried it with numbers and letters in Column J.
Book1
ABCDEFGHIJKL
2num1num2num3num4num5num1num2num3num4num10num11num12
363243920301425DOWN999321456
436242025182615RIGHT888321456
572242025182615RIGHT777321456
645243920401441LEFT666321456
754243926402741UP444321456
881243920401441LEFT333321456
927243920301425DOWN222321456
1018243926402741UP111321456
Main
Cell Formulas
RangeFormula
I3:I10I3=IF(AND(E3>C3,G3>E3,F3>D3,H3>F3),"UP",IF(AND(C3>E3,E3>G3,D3>F3,F3>H3),"DOWN",IF(AND(E3>C3,G3>E3,D3>F3,F3>H3),"RIGHT",IF(AND(C3>E3,E3>G3,F3>D3,H3>F3),"LEFT","NO"))))
 
Upvote 0

Forum statistics

Threads
1,224,259
Messages
6,177,482
Members
452,782
Latest member
ZCapitao

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