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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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