Hi,
I have below code to take the unique values from a selected column and show the count and percentage of each unique values in the total rows of data.
Request help on the area where the looping happens from first row to last row for all the unique values which make the result slow / even excel stuck if the number of unique values are more. For example column "ID". Can somebody help me with a better code to increase the speed of results.
I have below code to take the unique values from a selected column and show the count and percentage of each unique values in the total rows of data.
Request help on the area where the looping happens from first row to last row for all the unique values which make the result slow / even excel stuck if the number of unique values are more. For example column "ID". Can somebody help me with a better code to increase the speed of results.
Code:
Sub ViewUnique_Click()
If combx_Fields.ListIndex < 0 Then
MsgBox "Oops! You missed to choose the column heading.", vbInformation
Exit Sub
End If
Dim j As Long, cntArray As Long, iUnique As Long, iTotal As Long
Dim aryList() As Variant
ReDim aryList(1 To numUnique + 2, 0 To 2)
aryList(1, 0) = "Unique Values"
aryList(1, 1) = "Count"
aryList(1, 2) = "Percentage"
Load UF_UniqueList
For iUnique = 1 To numUnique
cntArray = 0
For j = rowFirst + 1 To rowLast
If UCase(wsData.Cells(j, ColumnPicked).Value) = UCase(UniqueList(iUnique)) Then
cntArray = cntArray + 1
End If
Next j
aryList(iUnique + 1, 0) = UniqueList(iUnique)
aryList(iUnique + 1, 1) = cntArray
iTotal = iTotal + cntArray
aryList(iUnique + 1, 2) = Format(cntArray / numRowsOfData, "0.00%")
Next iUnique
aryList(numUnique + 2, 0) = "Total"
aryList(numUnique + 2, 1) = iTotal
aryList(numUnique + 2, 2) = "100%"
UF_UniqueList.ListBox1.ColumnCount = UBound(aryList, 2) + 1
UF_UniqueList.ListBox1.List = aryList
UF_UniqueList.Show
End Sub