No error in VBA code but sort not functioning properly

AlabasterShelf

New Member
Joined
Jun 20, 2014
Messages
5
Hello all,

I have a spreadsheet that is pulling in query data. My VBA code refreshes the query, populates a formulas in two column, and finally sorts the data.

The query refresh is fine. The formula population is fine. The trouble I am having is that the sorting is not behaving. I don't get an error message. And the filter icon has the little triangle that shows excel thinks the column is sorted. But it is not.

In the specific column of interest, I am trying to remove the blank dates and sort oldest to newest. I used the macro recorder to generate the code seen here. I dropped message box indicators in each line of code, and they all worked fine so I know the code is cycling through properly. But it still isn't sorting. As I mentioned, I don't get any kind of error code with this. Anyone have any ideas?

Code:
'sort data
<today(),""late"",""""))"
ActiveWorkbook.Worksheets("OPERATION").ListObjects("OPER").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("OPERATION").ListObjects("OPER").Sort.SortFields.Add _
    Key:=Range("OPER[[#All],[WANT_DATE]]"), SortOn:=xlSortOnValues, Order:= _
    xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("OPERATION").ListObjects("OPER").Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
ThisWorkbook.Worksheets("OPERATION").ListObjects("OPER").Range.AutoFilter Field:=4, Criteria1:="<>"
</today(),""late"",""""))"
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,026
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
You didn't paste your code above, but it may be that you need to 'encourage' excel to re-filter. Try adding this line to your code after you have finished setting the filters (replace Sheet1 as appropriate):

Code:
<code class="vb spaces">   </code><code class="vb plain">Sheets(</code><code class="vb string">"Sheet1"</code><code class="vb plain">).AutoFilter.ApplyFilter</code>
 

AlabasterShelf

New Member
Joined
Jun 20, 2014
Messages
5
Argh! It was pasted in the post creator but didn't translate. Let me try again here. I did try the ApplyFilter line and it still did not work.

Code:
'sort data
ThisWorkbook.Worksheets("OPERATION").Activate
ActiveWorkbook.Worksheets("OPERATION").ListObjects("OPER").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("OPERATION").ListObjects("OPER").Sort.SortFields.Add _
    Key:=Range("OPER[[#All],[WANT_DATE]]"), SortOn:=xlSortOnValues, Order:= _
    xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("OPERATION").ListObjects("OPER").Range.AutoFilter Field:=4, Criteria1:="<>"
ThisWorkbook.Worksheets("OPERATION").AutoFilter.ApplyFilter
 

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,026
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
You are setting sort criteria, but not actually telling it to sort. Add this line:

Code:
 ActiveWorkbook.Worksheets("OPERATION").AutoFilter.Sort.Apply
 

AlabasterShelf

New Member
Joined
Jun 20, 2014
Messages
5
You are setting sort criteria, but not actually telling it to sort.

Isn't that what this section is doing? The very last line says .Apply

Code:
    With ActiveWorkbook.Worksheets("OPERATION").ListObjects("OPER").Sort       
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,026
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
That does indeed do it - but if you look above at your previous posts, this section of code has not been revealed to me. Please post the full code so I can help further.
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,109,027
Messages
5,526,329
Members
409,696
Latest member
EERS

This Week's Hot Topics

Top