VBA to sort by alphabetical order but not "Totals"

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
890
Hi all, I would like to write a VBA code so that, to sort by alphabetical order, by “CATEGORY” heading, beginning from row “A5” but excluding the last row which is the “Totals” row. I present below the original data and the expected result.
Thanking you in advance​

ORIGINAL DATA
[TABLE="class: MsoNormalTable, width: 520"]
<tbody>[TR]
[TD="width: 32, align: center"][/TD]
[TD="width: 195"]
A
[/TD]
[TD="width: 92"]
B
[/TD]
[TD="width: 92"]
C
[/TD]
[TD="width: 92"]
D
[/TD]
[TD="width: 92"]
E
[/TD]
[TD="width: 92"]
F
[/TD]
[TD="width: 92"]
G
[/TD]
[TD="width: 92"]
H
[/TD]
[/TR]
[TR]
[TD="width: 32"]
4

[/TD]
[TD="width: 195"]
CATEGORY
[/TD]
[TD="width: 92"]
JAN COST
[/TD]
[TD="width: 92"]
FEB COST
[/TD]
[TD="width: 92"]
MAR COST
[/TD]
[TD="width: 92"]
APR COST
[/TD]
[TD="width: 92"]
MAY COST
[/TD]
[TD="width: 92"]
JUN COST
[/TD]
[TD="width: 92"]
JUL COST
[/TD]
[/TR]
[TR]
[TD="width: 32"]
5
[/TD]
[TD="width: 195"]
SOFT DRINKS
[/TD]
[TD="width: 92"]
1,501.00
[/TD]
[TD="width: 92"]
1,756.00
[/TD]
[TD="width: 92"]
1,874.00
[/TD]
[TD="width: 92"]
2,105.00
[/TD]
[TD="width: 92"]
2,325.00
[/TD]
[TD="width: 92"]
3,128.00
[/TD]
[TD="width: 92"]
4,258.00
[/TD]
[/TR]
[TR]
[TD="width: 32"]
6
[/TD]
[TD="width: 195"]
SPIRITS & ALCOHOLICS
[/TD]
[TD="width: 92"]
855.00
[/TD]
[TD="width: 92"]
910.00
[/TD]
[TD="width: 92"]
1,241.00
[/TD]
[TD="width: 92"]
1,105.00
[/TD]
[TD="width: 92"]
1,855.00
[/TD]
[TD="width: 92"]
1,458.00
[/TD]
[TD="width: 92"]
1,785.00
[/TD]
[/TR]
[TR]
[TD="width: 32"]
7

[/TD]
[TD="width: 195"]
CEREALS & BAKERIES
[/TD]
[TD="width: 92"]
457.00
[/TD]
[TD="width: 92"]
589.00
[/TD]
[TD="width: 92"]
659.00
[/TD]
[TD="width: 92"]
478.00
[/TD]
[TD="width: 92"]
558.00
[/TD]
[TD="width: 92"]
378.00
[/TD]
[TD="width: 92"]
459.00
[/TD]
[/TR]
[TR]
[TD="width: 32"]
8
[/TD]
[TD="width: 195"]
HOT BEVERAGES
[/TD]
[TD="width: 92"]
455.00
[/TD]
[TD="width: 92"]
357.00
[/TD]
[TD="width: 92"]
378.00
[/TD]
[TD="width: 92"]
310.00
[/TD]
[TD="width: 92"]
278.00
[/TD]
[TD="width: 92"]
290.00
[/TD]
[TD="width: 92"]
283.00
[/TD]
[/TR]
[TR]
[TD="width: 32"]
9
[/TD]
[TD="width: 195"]
APERITIFS
[/TD]
[TD="width: 92"]
125.00
[/TD]
[TD="width: 92"]
220.00
[/TD]
[TD="width: 92"]
142.00
[/TD]
[TD="width: 92"]
238.00
[/TD]
[TD="width: 92"]
345.00
[/TD]
[TD="width: 92"]
248.00
[/TD]
[TD="width: 92"]
275.00
[/TD]
[/TR]
[TR]
[TD="width: 32"]
10

[/TD]
[TD="width: 195, align: center"][/TD]
[TD="width: 92"]
3,393.00
[/TD]
[TD="width: 92"]
3,832.00
[/TD]
[TD="width: 92"]
4,294.00
[/TD]
[TD="width: 92"]
4,236.00
[/TD]
[TD="width: 92"]
5,361.00
[/TD]
[TD="width: 92"]
5,502.00
[/TD]
[TD="width: 92"]
7,060.00
[/TD]
[/TR]
</tbody>[/TABLE]


EXPECTED RESULT
[TABLE="class: MsoNormalTable, width: 514"]
<tbody>[TR]
[TD="width: 32, align: center"][/TD]
[TD="width: 184"]
A
[/TD]
[TD="width: 92"]
B
[/TD]
[TD="width: 92"]
C
[/TD]
[TD="width: 92"]
D
[/TD]
[TD="width: 92"]
E
[/TD]
[TD="width: 92"]
F
[/TD]
[TD="width: 92"]
G
[/TD]
[TD="width: 92"]
H
[/TD]
[/TR]
[TR]
[TD="width: 32"]
4

[/TD]
[TD="width: 184"]
CATEGORY
[/TD]
[TD="width: 92"]
JAN COST
[/TD]
[TD="width: 92"]
FEB COST
[/TD]
[TD="width: 92"]
MAR COST
[/TD]
[TD="width: 92"]
APR COST
[/TD]
[TD="width: 92"]
MAY COST
[/TD]
[TD="width: 92"]
JUN COST
[/TD]
[TD="width: 92"]
JUL COST
[/TD]
[/TR]
[TR]
[TD="width: 32"]
5
[/TD]
[TD="width: 184"]
APERITIFS
[/TD]
[TD="width: 92"]
125.00
[/TD]
[TD="width: 92"]
220.00
[/TD]
[TD="width: 92"]
142.00
[/TD]
[TD="width: 92"]
238.00
[/TD]
[TD="width: 92"]
345.00
[/TD]
[TD="width: 92"]
248.00
[/TD]
[TD="width: 92"]
275.00
[/TD]
[/TR]
[TR]
[TD="width: 32"]
6
[/TD]
[TD="width: 184"]
HOT BEVERAGES
[/TD]
[TD="width: 92"]
455.00
[/TD]
[TD="width: 92"]
357.00
[/TD]
[TD="width: 92"]
378.00
[/TD]
[TD="width: 92"]
310.00
[/TD]
[TD="width: 92"]
278.00
[/TD]
[TD="width: 92"]
290.00
[/TD]
[TD="width: 92"]
283.00
[/TD]
[/TR]
[TR]
[TD="width: 32"]
7

[/TD]
[TD="width: 184"]
CEREALS & BAKERIES
[/TD]
[TD="width: 92"]
457.00
[/TD]
[TD="width: 92"]
589.00
[/TD]
[TD="width: 92"]
659.00
[/TD]
[TD="width: 92"]
478.00
[/TD]
[TD="width: 92"]
558.00
[/TD]
[TD="width: 92"]
378.00
[/TD]
[TD="width: 92"]
459.00
[/TD]
[/TR]
[TR]
[TD="width: 32"]
8
[/TD]
[TD="width: 184"]
SOFT DRINKS
[/TD]
[TD="width: 92"]
1,501.00
[/TD]
[TD="width: 92"]
1,756.00
[/TD]
[TD="width: 92"]
1,874.00
[/TD]
[TD="width: 92"]
2,105.00
[/TD]
[TD="width: 92"]
2,325.00
[/TD]
[TD="width: 92"]
3,128.00
[/TD]
[TD="width: 92"]
4,258.00
[/TD]
[/TR]
[TR]
[TD="width: 32"]
9
[/TD]
[TD="width: 184"]
SPIRITS & ALCOHOLICS
[/TD]
[TD="width: 92"]
855.00
[/TD]
[TD="width: 92"]
910.00
[/TD]
[TD="width: 92"]
1,241.00
[/TD]
[TD="width: 92"]
1,105.00
[/TD]
[TD="width: 92"]
1,855.00
[/TD]
[TD="width: 92"]
1,458.00
[/TD]
[TD="width: 92"]
1,785.00
[/TD]
[/TR]
[TR]
[TD="width: 32"]
10

[/TD]
[TD="width: 184, align: center"][/TD]
[TD="width: 92"]
3,393.00
[/TD]
[TD="width: 92"]
3,832.00
[/TD]
[TD="width: 92"]
4,294.00
[/TD]
[TD="width: 92"]
4,236.00
[/TD]
[TD="width: 92"]
5,361.00
[/TD]
[TD="width: 92"]
5,502.00
[/TD]
[TD="width: 92"]
7,060.00
[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
If you need a macro for that you can easily record one. It's just a normal sort
 
Upvote 0
Is it okay that, will not sort the last one which row which contains the "Totals"? How can i exclude the row with "Totals"?
 
Upvote 0
Because Col A is blank on the totals row, it will automatically be at the bottom
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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