Wednesday, 21 August 2013

Sorting values of an excel column by max occurrences using VB.net

Sorting values of an excel column by max occurrences using VB.net

I have an excel file which has column B1 to B500 (may vary) filled with
numbers. For example:
![sample data][1]
I need the output to be like:
![sample output][2]
I have this much code till now:
Sub Max()
Dim i As Long, j As Long
Dim cl As Excel.Range
i = 1
j = 1
For i = sheet.UsedRange.Rows.Count To 1 Step -1
cl = sheet.Cells(i, 2) '## Examine the cell in Column B
If xl.WorksheetFunction.CountIf(sheet.Range("B:B"), cl.Value) > 1
Then
cl.Value = sheet.Cells(j, 3).value 'copy to Column C
End If
j = j + 1
Next i
End Sub
What this code does is to find duplicates in column B and remove other
entries from the column. Nothing gets written in column C. I want the
column B to be unedited at the end. Also cannot figure out how to achieve
the sorting here.
Please help.

No comments:

Post a Comment