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"",""""))"
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,060
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,060
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,060
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:

Forum statistics

Threads
1,171,043
Messages
5,873,444
Members
432,980
Latest member
KMorrison12345

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
Top