Sort columns by occurance

excel_noob_77

New Member
Joined
Jun 26, 2017
Messages
6
Hey guys,

I am trying to sort a column by the occurance (or count) of a particular word. So for example if I had a column similar to this

Cat
Cat
Cat
Dog
Dog
Dog
Dog

It would sort the column based on their count (the items with the most occurance would be listed first and so forth). So it would be

Dog
Dog
Dog
Dog
Cat
Cat
Cat

Already happened to find a partial code which does a count similar to the code shown below. How should i modify this VBA code to then sort the column by frequency/count of a particular word. Could someone help me out with this.

Code:
Sub SortMessageByCount()
Dim Mode_Client As String
Range("A" & Rows.Count).FormulaArray = _
"=INDEX(A2:A26,MODE(MATCH(A2:A26,A2:A26,0)))"
Mode_Client = Range("A" & Rows.Count)
Range("A" & Rows.Count).ClearContents
MsgBox "The message occuring the most is " & Mode_Client
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
a0.001
Cat5col B is a count of col A value in A2:A18A0.004
Cat5b0.007
Cat5plus VALUE OF FIRST TWO LETTERS IN NAME FROM LOOKUP TABLEB0.01
Dog5this solves problem of duplicationsc0.013
Dog5C0.016
Dog5d0.019
Dog5D0.022
ant7e0.025
ant7E0.028
cat5f0.031
ant7F0.034
ant7g0.037
ant7G0.04
ant7h0.043
Cat5H0.046
Dog5I0.049
ant7I0.052
j0.055
J0.058
k0.061
ant77 derived by large(B2:B18,1)ant found by offset matchK0.064
ant7APPLIED TO TOP TABLEl0.067
ant7L0.07
ant7m0.073
ant7M0.076
ant7n0.079
Dog5N0.082
Dog5o0.085
Dog5O0.088
Dog5p0.091
Dog5P0.094
Cat5q0.097
Cat5Q0.1
Cat5r0.103
Cat5R0.106
Cat5s0.109
S0.112
t0.115
T0.118
u0.121
U0.124
v0.127
V0.13
w0.133
W0.136
x0.139
X0.142
X0.145
y0.148
Y0.151
z0.154

<colgroup><col width="64" span="16" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
This code will add a new temporary column to count the occurrences, sort on that column and then clear the column:

Code:
Public Sub SortByOccurrence()

Const SortColumn = "A" ' Column A - change to suit

Dim lastRow As Long
Dim lastCol As Long
Dim sortRange As Range
Dim cellRange As Range
Dim thisSheet As Worksheet

' Set the sheet
Set thisSheet = ActiveSheet

' Find the last row
lastRow = thisSheet.Cells(thisSheet.Rows.Count, SortColumn).End(xlUp).Row

' Find the last column
lastCol = thisSheet.Cells(1, thisSheet.Columns.Count).End(xlToLeft).Column + 1

' Set the ranges
Set cellRange = thisSheet.Range(thisSheet.Cells(1, SortColumn), thisSheet.Cells(lastRow, SortColumn))
Set sortRange = thisSheet.Range(thisSheet.Cells(1, lastCol), thisSheet.Cells(lastRow, lastCol))

' Create a new column with the formula
sortRange.Formula = "=COUNTIF(" & cellRange.Address & ",$" & SortColumn & "1)"

' Now do the sorting
With thisSheet.Sort
    .SortFields.Clear
    .SortFields.Add Key:=sortRange, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    .SortFields.Add Key:=cellRange, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange thisSheet.Range(Cells(1, SortColumn), Cells(lastRow, lastCol))
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlSortColumns
    .SortMethod = xlPinYin
    .Apply
End With

' Clear the temporary column
sortRange.ClearContents

End Sub

Change the SortColumn to be the column that you're sorting. I've assumed no column headers.

WBD
 
Upvote 0
So how would i translate that into VBA code is that by having. Sorry im quite new to this. Would it be something similar to this.

Code:
Sub SortMessageByArea()
Dim Mode_Client As String
Range("C" & Rows.Count).FormulaArray = _
"=[B]OFFSET[/B](INDEX(B2:B26,MODE(MATCH(B2:B26,B2:B26,0))),-1,0)"
Mode_Client = Range("C" & Rows.Count)
Range("C" & Rows.Count).ClearContents
Range("C1").Sort Key1:=Range("C2"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
MsgBox "The message occuring the most by area is " & Mode_Client
End Sub
 
Upvote 0
Cheers this work fine. Just one issue though how would i sort columns right of the active column cause it sort all the columns to the left of the selection. How would i modify the VBA to consider all the columns in the sheet.
 
Upvote 0
Change the .SetRange line to sort all columns on the sheet:

Rich (BB code):
    .SetRange thisSheet.Range(Cells(1, 1), Cells(lastRow, lastCol))

WBD
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

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