Sub Ghep_Chuoi2()
Dim nc As Long, j As Long, r As Long, i As Long
Dim ar() As Long
Dim max() As Long
Dim str As String
nc = InputBox("Nhap so cot:")
ReDim ar(nc) As Long
ReDim max(nc) As Long
For i = 1 To nc
max(i) = Cells(65536, i).End(xlUp).Row
ar(i) = 3
Next
r = 3
Do While i > 0
str = ""
For j = 1 To nc
str = str & " " & Cells(ar(j), j)
Next
Cells(r, nc + 1) = Application.WorksheetFunction.Trim(str)
r = r + 1
If ar(nc) = max(nc) Then
i = nc
Do Until ar(i) < max(i) Or i = 0
ar(i) = 3
i = i - 1
Loop
ar(i) = ar(i) + 1
ar(nc) = 2
End If
ar(nc) = ar(nc) + 1
Loop
End Sub