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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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>
 
Upvote 0
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
 
Upvote 0
You are setting sort criteria, but not actually telling it to sort. Add this line:

Code:
 ActiveWorkbook.Worksheets("OPERATION").AutoFilter.Sort.Apply
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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