Option Explicit
Dim arrSort() As String
Sub NormalizeColumns()
Dim ws As Worksheet, rng As Range
Dim lastRow As Long, lastCol As Long
Dim col As Long, r As Long
Dim dict As Object
Dim cellValue As String
Dim arr(), arrSrc, arrResult(10, 5) As String, i As Long, j As Long, k As Long
Set ws = ActiveSheet
Set rng = ws.Range("A1:C5")
arrSrc = rng
lastRow = UBound(arrSrc, 1): lastCol = UBound(arrSrc, 2)
Call TongHopVaSapXep
For k = 1 To UBound(arrSrc, 2)
For i = 0 To UBound(arrSort)
'Debug.Print arrSort(i), arrSrc(i + 1, 1)
For j = 1 To UBound(arrSrc, 1)
Debug.Print " --" & arrSrc(j, 1)
If arrSort(i) = arrSrc(j, k) Then
arrResult(i, k - 1) = arrSrc(j, k)
Exit For
Else
arrResult(i, k - 1) = "-"
End If
Next j
Next i
Next k
Worksheets("Sheet1").Range("A8").Resize(UBound(arrResult, 1) + 1, UBound(arrResult, 2) + 1).Value = arrResult
End Sub
Sub TongHopVaSapXep()
Dim rng As Range, cell As Range, temp As String
Dim dict As Object
Dim i As Long, j As Long, outputCol As Long
Set rng = Sheet1.Range("A1:C5")
Set dict = CreateObject("Scripting.Dictionary")
' Loc và thêm giá tri không trùng
For Each cell In rng
If Trim(cell.Value) <> "" Then
If Not dict.exists(cell.Value) Then
dict.Add cell.Value, 1
End If
End If
Next cell
'Chuyen thanh mang de so sanh
ReDim arrSort(0 To dict.Count - 1)
For i = 0 To dict.Count - 1
arrSort(i) = dict.Keys()(i)
Next i
' Bubble sort
For i = 0 To UBound(arrSort) - 1
For j = i + 1 To UBound(arrSort)
If arrSort(i) > arrSort(j) Then
temp = arrSort(i)
arrSort(i) = arrSort(j)
arrSort(j) = temp
End If
Next j
Next i
End Sub