VBA code to auto sort table based on any change within a specific column

duteberta

Board Regular
Joined
Jun 14, 2009
Messages
92
Office Version
  1. 365
Platform
  1. MacOS
Given: Table Name = "MASTER"
Given: Column where change would trigger macro = "Status" (currently column 3 of table)
Screenshot 2024-03-16 at 10.30.54 AM.png


Given: Sort Macro =

VBA Code:
Sub SortTable()

    Dim iSheet As Worksheet
    Dim iTable As ListObject
    Dim iColumn As Range
   
    Set iSheet = ActiveSheet
    Set iTable = iSheet.ListObjects("MASTER")
    Set iColumn1 = Range("MASTER[Status]")
    Set iColumn2 = Range("MASTER[CloseD]")
    Set iColumn3 = Range("MASTER[C1]")
   
    With iTable.Sort
   
      .SortFields.Clear
            .SortFields.Add Key:=iColumn1, Order:=xlAscending
            .SortFields.Add Key:=iColumn2, Order:=xlDescending
            .SortFields.Add Key:=iColumn3, Order:=xlAscending
            .Header = xlYes
            .Apply
        End With

End Sub
+++++++

Question: How do I write VBA code to trigger the above macro? I know the macro works because I've tested it. However I cannot get this trigger based on cell change in column 3.

Context: I already have another script running on the same worksheet that I'm worried is conflicting with what I'm attempting to do....


++++

Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim tbl             As ListObject
    Dim rngCell         As Range
    
    If Target.Cells.Count > 1 Then Exit Sub
    
    On Error Resume Next
    Set tbl = Target.Worksheet.ListObjects(1)
    If tbl Is Nothing Then Exit Sub
    If tbl.ListRows.Count = 0 Then Exit Sub
    
    Set rngCell = Application.Intersect(tbl.ListColumns("X").DataBodyRange, Target)
    If rngCell Is Nothing Then Exit Sub
    On Error GoTo 0
    
    tbl.ListColumns("X").DataBodyRange = ""
    rngCell.Value = 1
End Sub
++++
 
Last edited by a moderator:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added tags (vba & rich) for you this time.

I'm not sure that you have given us enough information for a full answer.

First code refers to table columns "CloseD" and "C1" but they are not included in your sample **

Second code refers to ListObjects1 but we don't know if that is the same or different to the .ListObjects("MASTER") referred to in the first code.
We don't know what ListColumns("X") in the second code is.

** When providing sample data I suggest that you investigate XL2BB to make it easier for helpers by not having to manually type out sample data to test with.
 
Upvote 0
OK I will re-submit question with the XL2BB- thanks for the suggestion
 
Upvote 0
TEST-vba.xlsm
BCDEFG
320242 PEND LMark PerrineChristy, Evelyn04/12/24
420245 ACT-SELLLonnie HasslerIreland, Brenda04/25/24
520246 PRE-LISTDarlene PerrineChristy, Evelyn05/28/24
620246 PRE-LISTMark PerrineBlakemore, Dickie05/03/24
720246 PRE-LISTMark PerrineBuffum, Stanley G05/03/24
820246 PRE-LISTLonnie HasslerChew, Tad R05/03/24
920246 PRE-LISTMark PerrineHenry, Paul D05/03/24
1020246 PRE-LISTMark PerrineLaPlant, June M05/03/24
1120248 FUTUREMark PerrineBlakemore, Dickie J
1220248 FUTUREMark PerrineLaPlant, June M
1320249 FUTURE LDarlene PerrineWoods, James12/31/24
1420249 FUTURE LDarlene PerrineZacchaeus, Josh02/08/24
1520249 FUTURE LLonnie HasslerChew, Tad R
1620249 FUTURE LMark PerrineEmerson, Steve
1720249 FUTURE LDarlene PerrineFillenwarth, Joseph
1820249 FUTURE LMark PerrineGood, Ursula E
1920249 FUTURE LDarlene PerrineHenry, Paul D
2020249 FUTURE LMark PerrineHogue, Rick E
2120249 FUTURE LDarlene PerrineHumpherys, Jeffrey
2220249 FUTURE LLonnie HasslerIreland, Mark
2320249 FUTURE LDarlene PerrineLicari, Connie
2420249 FUTURE LLonnie HasslerOlin, Franklin
2520249 FUTURE LLonnie HasslerRector, Katlyn
2620249 FUTURE LLonnie HasslerWert, Dee
2720249 FUTURE LDarlene PerrineWest, Nikki
2820249 FUTURE LDarlene PerrineWurtz, Larry
292050EXP/WITH/TERMLonnie HasslerZakasourus, Tester Z12/31/24
302024SOLDLonnie HasslerHancock, Deborah J03/15/24
MASTER
Cell Formulas
RangeFormula
C30C30=IF([@CloseD]>0,YEAR([@CloseD]),#REF!)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C3:G30Expression=IF(AND($B3=1,OR($D3="1 PENDING",$D3="2 PEND L",$D3="3 OFFERED")),TRUE, FALSE)textNO
C3:G30Expression=IF(AND($B3=1,OR($D3<>"1 PENDING",$D3<>"2 PEND L",$D3<>"3 OFFERED")),TRUE, FALSE)textNO
B3:B30Expression=$B3=1textNO
B3:G30Expression=$D3="1 PENDING"textNO
B3:G30Expression=$D3="2 PEND L"textNO
B3:G30Expression=$D3="3 OFFERED"textNO
B3:G30Expression=$D3="4 ACT-BUY"textNO
B3:G30Expression=$D3="5 ACT-SELL"textNO
B3:G30Expression=$D3="6 PRE-LIST"textNO
B3:G30Expression=$D3="7 TOM"textNO
B3:G30Expression=$D3="8 FUTURE"textNO
B3:G30Expression=$D3="9 FUTURE L"textNO
B3:G30Expression=$D3="SOLD"textNO
Cells with Data Validation
CellAllowCriteria
D3:D30List1 PENDING, 2 PEND L, 3 OFFERED, 4 ACT-BUY, 5 ACT-SELL, 6 PRE-LIST, 7 TOM, 8 FUTURE, 9 FUTURE L, SOLD, EXP/WITH/TERM
E3:E1048576ListMark Perrine, Darlene Perrine, Lonnie Hassler
 
Upvote 0
I am not concerned with the Conditional Formatting - only the VBA code for sorting on cell change.
 
Upvote 0
Thanks for the XL2BB but unfortunately no headings so we still don't really know what column is what. Could we have XL2BB again but include headings and all columns relevant to any of the formulas or conditional formatting?

Also, can you confirm how the values in the "trigger column" (Status) will be changed? (eg Manual Entry, Changed by vba code, changed as a result of a formula recalculation)
 
Upvote 0
Yikes I just realized the headers didn't come over. The trigger would be the dropdown in the "Status" column selecting any value.


Screenshot 2024-03-18 at 9.33.17 AM.png
 
Upvote 0
Thanks. Unless you already have a Worksheet_Change event code in the code module of sheet 'MASTER', all you should need is this in the 'MASTER' worksheet's module.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("MASTER[Status]")) Is Nothing Then SortTable
End Sub

If you do already have a Worksheet_Change event code in the code module of sheet 'MASTER', then please post that code (remembering my comment at the beginning of post 2)
 
Upvote 0
I think my problem is that I have two codes running under the "Private Sub Worksheet_Change(ByVal Target As Range)" in the same worksheet and I'm not sure how to arrange them so they do not conflict with each other.
  1. One is a currently working script: Changing row to values (gets rid of underlying functions in row cells)
  2. And the second one is what I'm trying to implement- the auto sort one dropdown change feature
Mayne this is just impossible to do. I'll just have to manually run SORT macro when I need.

Thanks for the help anyway.
 
Upvote 0
Mayne this is just impossible to do.
It should not be impossible.


I have two codes running under the "Private Sub Worksheet_Change(ByVal Target As Range)"
As I said in my previous post ...
If you do already have a Worksheet_Change event code in the code module of sheet 'MASTER', then please post that code (remembering my comment at the beginning of post 2)
Once I can see your existing code (Changing row to values), I can investigate how to combine the two.
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,088
Members
453,021
Latest member
Justyna P

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