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)
 
Ok thanks I have also re named "Number" to "Frequency" like this:

Set rNumberHeaderCell = .Cells.Find("Frequency", LookIn:=xlValues, SearchOrder:=xlByColumns)

I am assuming this script line finds the column to sort. When I run the VBA it is sorting the column however its not sorting column G so the numbers are becoming mispalced.

After the sort:

the project.xlsx
GH
1NumberFrequency
2234
3265
416
5166
6256
797
8177
9317
10298
1128
1278
13249
1489
15129
16159
172810
18510
192210
203310
213610
22311
23411
24611
253211
262712
271412
281912
292012
302112
311813
323413
331113
343514
351015
361315
373018
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,31)
H10H10=COUNTIF(Data!D:D,29)
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,5)
H19H19=COUNTIF(Data!D:D,22)
H20H20=COUNTIF(Data!D:D,33)
H21H21=COUNTIF(Data!D:D,36)
H22H22=COUNTIF(Data!D:D,3)
H23H23=COUNTIF(Data!D:D,4)
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,19)
H29H29=COUNTIF(Data!D:D,20)
H30H30=COUNTIF(Data!D:D,21)
H31H31=COUNTIF(Data!D:D,18)
H32H32=COUNTIF(Data!D:D,34)
H33H33=COUNTIF(Data!D:D,11)
H34H34=COUNTIF(Data!D:D,35)
H35H35=COUNTIF(Data!D:D,13)
H36H36=COUNTIF(Data!D:D,30)
H37H37=COUNTIF(Data!D:D,10)



Before the sort :

the project.xlsx
GH
1NumberFrequency
2234
3265
416
5166
6256
797
8177
9317
10298
1128
1278
13249
1489
15129
16159
172810
18510
192210
203310
213610
22311
23411
24611
253211
262712
271412
281912
292012
302112
311813
323413
331113
343514
351018
361315
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,31)
H10H10=COUNTIF(Data!D:D,29)
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,5)
H19H19=COUNTIF(Data!D:D,22)
H20H20=COUNTIF(Data!D:D,33)
H21H21=COUNTIF(Data!D:D,36)
H22H22=COUNTIF(Data!D:D,3)
H23H23=COUNTIF(Data!D:D,4)
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,19)
H29H29=COUNTIF(Data!D:D,20)
H30H30=COUNTIF(Data!D:D,21)
H31H31=COUNTIF(Data!D:D,18)
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)



Column G number 10 has a mismatch
 
Last edited:
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
First, it seems that the before and after you provided are the same?

I am curious, why did you change that part of the code? What are you trying to accomplish by doing that?

Anyway, as far as I can tell from your post that is the problem. The way the code is set up it looks for the Number column in column G and then assumes that the Frequency column is next to it in column H. By changing the header text sought -- from Number to Frequency -- the data in the Frequency column (column H) is treated as Number column by the sort and the Frequency column then becomes column I.
 
Upvote 0
Your code worked perfect Changing the name of the Column header caused the issue thanks so much for the code.
 
Upvote 0

Forum statistics

Threads
1,215,364
Messages
6,124,507
Members
449,166
Latest member
hokjock

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