Macro for Excel Automatically Sort When Data Changes

keranali

Rules Violation
Joined
Oct 4, 2010
Messages
234
Office Version
  1. 365
Platform
  1. Windows
need help with a macro or formula to automatically sort data as data is changed

the project.xlsx
GH
1Number Frequency
2234
3265
416
5166
6256
797
8177
9297
10317
1128
1278
13248
1489
15129
16159
17289
18310
19410
20510
21610
222210
233310
243610
252711
263211
271412
281812
291912
302012
312112
323412
331113
343513
351014
361314
373015
Data
Cell Formulas
RangeFormula
H2H2=COUNTIF(Data!D:D,23)
H3H3=COUNTIF(Data!D:D,26)
H4H4=COUNTIF(Data!D:D,1)
H5H5=COUNTIF(Data!D:D,16)
H6H6=COUNTIF(Data!D:D,25)
H7H7=COUNTIF(Data!D:D,9)
H8H8=COUNTIF(Data!D:D,17)
H9H9=COUNTIF(Data!D:D,29)
H10H10=COUNTIF(Data!D:D,31)
H11H11=COUNTIF(Data!D:D,2)
H12H12=COUNTIF(Data!D:D,7)
H13H13=COUNTIF(Data!D:D,24)
H14H14=COUNTIF(Data!D:D,8)
H15H15=COUNTIF(Data!D:D,12)
H16H16=COUNTIF(Data!D:D,15)
H17H17=COUNTIF(Data!D:D,28)
H18H18=COUNTIF(Data!D:D,3)
H19H19=COUNTIF(Data!D:D,4)
H20H20=COUNTIF(Data!D:D,5)
H21H21=COUNTIF(Data!D:D,6)
H22H22=COUNTIF(Data!D:D,22)
H23H23=COUNTIF(Data!D:D,33)
H24H24=COUNTIF(Data!D:D,36)
H25H25=COUNTIF(Data!D:D,27)
H26H26=COUNTIF(Data!D:D,32)
H27H27=COUNTIF(Data!D:D,14)
H28H28=COUNTIF(Data!D:D,18)
H29H29=COUNTIF(Data!D:D,19)
H30H30=COUNTIF(Data!D:D,20)
H31H31=COUNTIF(Data!D:D,21)
H32H32=COUNTIF(Data!D:D,34)
H33H33=COUNTIF(Data!D:D,11)
H34H34=COUNTIF(Data!D:D,35)
H35H35=COUNTIF(Data!D:D,10)
H36H36=COUNTIF(Data!D:D,13)
H37H37=COUNTIF(Data!D:D,30)
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I believe that I was able to set up a workbook that does what is wanted. Get it HERE.

I hope that you know what the worksheet change event is? Code uses that.

VBA Code:
'Code module for the worksheet named Data.
'Worksheet_Change event.
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim bDoSort As Boolean
    
    Dim rIntersectRange As Range
    
    Dim rDataToSortRange As Range
    
'   Anytime any cell in column D changes the sort will occur.
    Set rIntersectRange = Intersect(Target, Range("D:D"))
    
    If Not rIntersectRange Is Nothing _
     Then
        Call SortOnChange
    End If

End Sub

VBA Code:
Option Explicit

Sub SortOnChange()

    Dim rNumberHeaderCell As Range 'Header cell containing Number for the data to sort.

    Dim rFreqHeaderCell As Range 'Header cell for the frequency column for dta to sort.

    Dim rSortRange As Range 'Range containg all data (cells) to sort.

    Dim iDataRows As Long  'Count of rows in the the range to sort
    
    Dim sMsg As String 'Used to tell user that code did not find the header
                       'cell containg the word "Number"
    
    With Worksheets("Data")
    
'       Look for the word Number in the worksheet named Data to determine where
'       the data to sort is.
        
        Set rNumberHeaderCell = .Cells.Find("Number", LookIn:=xlValues, SearchOrder:=xlByColumns)
        
        If rNumberHeaderCell Is Nothing _
         Then
         
            sMsg = "Could not find a cell containing the" _
            & Chr(10) _
            & "word Number in the worksheet that is named" _
            & Chr(10) _
            & "Data so we cannot find the data to sort!"
            
            MsgBox sMsg, vbCritical
            
        Else
        
            iDataRows = rNumberHeaderCell.Offset(100000).End(xlUp).Row - rNumberHeaderCell.Row
            
'           Range object points to the data (cells) to be sorted.
            Set rSortRange = rNumberHeaderCell.Resize(iDataRows + 1, 2)
            
'           Single celled "header" above cells containing frequency data.
            Set rFreqHeaderCell = rNumberHeaderCell.Offset(0, 1)
        
            With .Sort

                .SortFields.Clear

                .SortFields.Add Key:=rFreqHeaderCell, Order:=xlAscending

                .SetRange rSortRange

                .Header = xlYes
                
                .Apply

            End With '.Sort

        End If
        
    End With

End Sub
 
Upvote 1
Solution
Your formulas look like this for example =COUNTIF(Data!D:D,30) which I do not understand. BUT, you did not provide the data in column D. So the formulas in column B always evaluate to #VALUE!
 
Upvote 0
Sorry for the bad explanation please see below it sorting dynamically on the frequency column when a new entry is inputted.

the project.xlsx
BCDFGH
1DateTimeNumNumber Frequency
22-Jan-23Morning33234
32-Jan-23Midday21265
42-Jan-23Afternoon416
52-Jan-23Evening1166
63-Jan-23Morning2256
73-Jan-23Midday1597
83-Jan-23Afternoon32177
93-Jan-23Evening22297
104-Jan-23Morning27317
114-Jan-23Midday528
124-Jan-23Afternoon3278
134-Jan-23Evening35248
145-Jan-23Morning1889
155-Jan-23Midday12129
165-Jan-23Afternoon15159
175-Jan-23Evening11289
186-Jan-23Morning19310
196-Jan-23Midday12410
206-Jan-23Afternoon2510
216-Jan-23Evening202210
227-Jan-23Morning363310
237-Jan-23Midday363610
247-Jan-23Afternoon5611
257-Jan-23Evening203211
269-Jan-23Morning322712
279-Jan-23Midday131412
289-Jan-23Afternoon51812
299-Jan-23Evening211912
3010-Jan-23Morning182012
3110-Jan-23Midday52112
3210-Jan-23Afternoon353412
3310-Jan-23Evening41113
3411-Jan-23Morning303514
3511-Jan-23Midday171014
3611-Jan-23Afternoon331314
3711-Jan-23Evening113015
3812-Jan-23Morning13
3912-Jan-23Midday33
4012-Jan-23Afternoon28
4112-Jan-23Evening33
4213-Jan-23Morning34
4313-Jan-23Midday21
4413-Jan-23Afternoon19
4513-Jan-23Evening8
4614-Jan-23Morning18
4714-Jan-23Midday11
4814-Jan-23Afternoon12
4914-Jan-23Evening19
5016-Jan-23Morning6
5116-Jan-23Midday13
5216-Jan-23Afternoon6
5316-Jan-23Evening10
5417-Jan-23Morning33
5517-Jan-23Midday3
5617-Jan-23Afternoon15
5717-Jan-23Evening22
5818-Jan-23Morning20
5918-Jan-23Midday26
6018-Jan-23Afternoon30
6118-Jan-23Evening4
6219-Jan-23Morning13
6319-Jan-23Midday12
6419-Jan-23Afternoon1
6519-Jan-23Evening14
6620-Jan-23Morning10
6720-Jan-23Midday19
6820-Jan-23Afternoon18
6920-Jan-23Evening28
7021-Jan-23Morning24
7121-Jan-23Midday30
7221-Jan-23Afternoon3
7321-Jan-23Evening32
7423-Jan-23Morning4
7523-Jan-23Midday20
7623-Jan-23Afternoon36
7723-Jan-23Evening10
7824-Jan-23Morning9
7924-Jan-23Midday10
8024-Jan-23Afternoon12
8124-Jan-23Evening21
8225-Jan-23Morning29
8325-Jan-23Midday8
8425-Jan-23Afternoon15
8525-Jan-23Evening21
8626-Jan-23Morning34
8726-Jan-23Midday5
8826-Jan-23Afternoon32
8926-Jan-23Evening17
9027-Jan-23Morning26
9127-Jan-23Midday6
9227-Jan-23Afternoon11
9327-Jan-23Evening13
9428-Jan-23Morning2
9528-Jan-23Midday2
9628-Jan-23Afternoon27
9728-Jan-23Evening14
9830-Jan-23Morning25
9930-Jan-23Midday19
10030-Jan-23Afternoon26
Data
Cell Formulas
RangeFormula
H2H2=COUNTIF(Data!D:D,23)
H3H3=COUNTIF(Data!D:D,26)
H4H4=COUNTIF(Data!D:D,1)
H5H5=COUNTIF(Data!D:D,16)
H6H6=COUNTIF(Data!D:D,25)
H7H7=COUNTIF(Data!D:D,9)
H8H8=COUNTIF(Data!D:D,17)
H9H9=COUNTIF(Data!D:D,29)
H10H10=COUNTIF(Data!D:D,31)
H11H11=COUNTIF(Data!D:D,2)
H12H12=COUNTIF(Data!D:D,7)
H13H13=COUNTIF(Data!D:D,24)
H14H14=COUNTIF(Data!D:D,8)
H15H15=COUNTIF(Data!D:D,12)
H16H16=COUNTIF(Data!D:D,15)
H17H17=COUNTIF(Data!D:D,28)
H18H18=COUNTIF(Data!D:D,3)
H19H19=COUNTIF(Data!D:D,4)
H20H20=COUNTIF(Data!D:D,5)
H21H21=COUNTIF(Data!D:D,22)
H22H22=COUNTIF(Data!D:D,33)
H23H23=COUNTIF(Data!D:D,36)
H24H24=COUNTIF(Data!D:D,6)
H25H25=COUNTIF(Data!D:D,32)
H26H26=COUNTIF(Data!D:D,27)
H27H27=COUNTIF(Data!D:D,14)
H28H28=COUNTIF(Data!D:D,18)
H29H29=COUNTIF(Data!D:D,19)
H30H30=COUNTIF(Data!D:D,20)
H31H31=COUNTIF(Data!D:D,21)
H32H32=COUNTIF(Data!D:D,34)
H33H33=COUNTIF(Data!D:D,11)
H34H34=COUNTIF(Data!D:D,35)
H35H35=COUNTIF(Data!D:D,10)
H36H36=COUNTIF(Data!D:D,13)
H37H37=COUNTIF(Data!D:D,30)
 
Upvote 0
You definitely left out some detail. When you say "when data changes" which data? Is sorting done only on cols G & H (based on frequency)? Any other detail that someone helping should know about?
 
Upvote 0
My apologies sorting will only be done on column H from lowest to Highest Data will be inputed 4 times per day on columns C & D. I can sort it manually however it will have to sort everytime an entry is inputted wanted it to dynamically sort.
 
Upvote 0
Attempting to understand the need, I it seems that if user changes something in column D then the sort triggers. Presumably, changes occur to data one row in cols B then C then D but until the value in col D is input the entry (B & C & D) is not complete. Should data sort if a row is deleted from a row of data in cols B through D?
 
Upvote 0
You are correct and yes if data is deleted it should update the sort, col B and C is not too important for the data sort which will take place on col H
 
Upvote 0
I made one change to the sort code. This was done sothat the Number values are sorted after the Frequency data so that the Number values are in order. Example. If Number values for frequency of 1 are 23, 13, 15, 24 they will now be in ascending order: 13, 15, 23, 24.

Replace this one line of code

VBA Code:
                .SortFields.Add Key:=rFreqHeaderCell, Order:=xlAscending

with these TWO lines of code.

VBA Code:
                .SortFields.Add Key:=rFreqHeaderCell, Order:=xlAscending
                
                .SortFields.Add Key:=rNumberHeaderCell, Order:=xlAscending '<= added key to sort.
 
Upvote 0

Forum statistics

Threads
1,216,085
Messages
6,128,733
Members
449,465
Latest member
TAKLAM

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