VBA Sort table based on cell text

Darlie247

New Member
Joined
Apr 2, 2022
Messages
16
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello everyone,

I'm still learning VBA and all it can do. And I have really appreciated the help I have received on this forum through the years. :)

My current question is coding for automatically sorting an excel table (as data is entered) when a cell from a specific column contains specific text. I have attached an image (my workplace does not allow for add-ons to be downloaded). I would like for the table to auto sort wherein the "Yes" rows are placed first at the top of the table. From what I am understanding is that it has to be written as a change event?

Any help is always appreciated :)
 

Attachments

  • Capture.PNG
    Capture.PNG
    10.5 KB · Views: 15
Assume you have 8 column, put this on your Worksheet_Change event :

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 8 Then
        Dim iLast As Long: iLast = Me.UsedRange.Rows.Count

        With Me
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=.Range("H1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            .Sort.SetRange .Range("A1:H" & iLast)
            .Sort.Header = xlYes
            .Sort.Apply
        End With
    End If
End Sub

It will give "Yes" in the top and "No" in the bottom.
 
Upvote 0
It appears that you are working with a formal Excel table (ListObject). In that case and assuming that the values in the "Reorder?" column are not the result of formulas then try (after editing the table name as required) ..

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("Table1[Reorder?]")) Is Nothing Then
    Application.ScreenUpdating = False
    With ActiveSheet.ListObjects("Table1").Sort
      .SortFields.Clear
      .SortFields.Add Key:=Range("Table1[[#All],[Reorder?]]"), SortOn:=xlSortOnValues, Order:=xlDescending
      .Header = xlYes
      .Orientation = xlTopToBottom
      .Apply
    End With
    Application.ScreenUpdating = True
  End If
End Sub

If the "Reorder?" column is populated by formulas then try

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("Table1")) Is Nothing Then
    Application.ScreenUpdating = False
    With ActiveSheet.ListObjects("Table1").Sort
      .SortFields.Clear
      .SortFields.Add Key:=Range("Table1[[#All],[Reorder?]]"), SortOn:=xlSortOnValues, Order:=xlDescending
      .Header = xlYes
      .Orientation = xlTopToBottom
      .Apply
    End With
    Application.ScreenUpdating = True
  End If
 
Upvote 0
Solution
Thanks to both of you! These both worked for me.

I appreciate @Peter_SSs that you included a code for formulas, I totally forgot that I had some in that column (I should have mentioned it originally! My bad 😬)
 
Upvote 0

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