excel vba on change events for sorting if changed from......

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
So I have the below code which works perfectly (maybe could be cleaned up a little but its working) what im trying to figure out if right now if column I changed then it runs the sort or if "ETOX-SKIP" is selected from the drop down in column D then it sorts.

The thing im trying to figure out is if there a way to trigger the event if column D is changed from "ETOX-SKIP" to anything else then trrigger the sort.

I dont wanna sort with every single change on the page just buy the two current criteria and as I said if it is changed from "ETOX-SKIP"

Thanks in advance.

Code:
Private Sub WorkSheet_Change(ByVal Target As Range)


Dim SS As Worksheet: Set SS = ThisWorkbook.Sheets("Steri Sheet")
Dim ST As ListObject: Set ST = SS.ListObjects("SteriTable")
Dim SteriS As Range: Set SteriS = Range("SteriTable[DEST LOC ID]")
Dim SteriST As Range: Set SteriST = Range("SteriTable[STATE]")
Dim SteriZ As Range: Set SteriZ = Range("SteriTable[ZIP CODE]")


On Error Resume Next
'---------------------Sort by Loc ID-----------------------------
If Not Intersect(Target, Range("I:I")) Is Nothing Then
    With ST.Sort
        .SortFields.Clear
        .SortFields.Add Key:=SteriS, SortOn:=xlSortOnValues, Order:=xlAscending _
        , CustomOrder:="ETOX,ETOXCNWY,ETOXNMTF,ETOXODFL,ETOXFXFE,ETOXLMEL,ETOXGGJ,ETOXMSP,ETOXREPL,EDC/WDC,ETOX-SKIP" _
        , DataOption:=xlSortNormal
        .SortFields.Add Key:=SteriST, SortOn:=xlSortOnValues, Order:=xlAscending _
        , DataOption:=xlSortNormal
        .SortFields.Add Key:=SteriZ, SortOn:=xlSortOnValues, Order:=xlAscending _
        , DataOption:=xlSortNormal
        .Apply
        Range("F10").Select
      End With
    End If
'----------------SOrt if ETOX-SKIP is selected-------------------------------------
  If Not Intersect(Target, Range("D:D")).Value = "ETOX-SKIP" Then
   Exit Sub
    Else
     With ST.Sort
         .SortFields.Clear
         .SortFields.Add Key:=SteriS, SortOn:=xlSortOnValues, Order:=xlAscending _
         , CustomOrder:="ETOX,ETOXCNWY,ETOXNMTF,ETOXODFL,ETOXFXFE,ETOXLMEL,ETOXGGJ,ETOXMSP,ETOXREPL,EDC/WDC,ETOX-SKIP" _
         , DataOption:=xlSortNormal
         .SortFields.Add Key:=SteriST, SortOn:=xlSortOnValues, Order:=xlAscending _
         , DataOption:=xlSortNormal
         .SortFields.Add Key:=SteriZ, SortOn:=xlSortOnValues, Order:=xlAscending _
         , DataOption:=xlSortNormal
         .Apply
         Range("F10").Select
       End With
    End If
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,213,496
Messages
6,113,993
Members
448,539
Latest member
alex78

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