Sub Macro1()
'Dim i As ...
'Dim NI1hdr As Range
'Dim lastcel As Range
'Dim NI1() As ...
'Dim NI1tmp() As ...
'Dim j As ...
'Dim MaxNbPack As ...
'Dim cel As Range
'Dim isinarray As Boolean
'Dim pack() As ...
'Dim fistfindrow As Integer
'Dim c As Range
'Dim packhdr As Range
'Dim packtmp() As ...
'Dim nbNI1 As ...
'Set NI1hdr = ...
'Set lastcel = ...
'Set packhdr - ...
' we are at the start. we make a list of all NI1.
i = 1
Do While NI1hdr.Offset(i, 0).Value = vbNullString 'loop until we find a non-empty cell (should be quick)
i = i + 1
If i = lastcel.Row Then MsgBox "No non-empty cells in the NI1 column: exiting macro now.": Exit Sub
Loop
ReDim Preserve NI1(0 To 0)
ReDim Preserve NI1tmp(0 To 0)
i = 0
MaxNbPack = 0
For Each cel In Range("I" & NI1hdr.Row + 1, "I" & lastcel.Row)
If cel Is Nothing Then Resume Next
isinarray = False
For k = LBound(NI1tmp) To UBound(NI1tmp)
If NI1tmp(k) = cel.Value Then isinarray = True 'check if NI1 is in the tmp array which mirrors the real one
Next k
j = 0
If (Not isinarray) And cel.Value <> "" Then 'copy the NI1 to the array in i-th position
'fill the pack array, then later assign it to the NI1 array
ReDim Preserve pack(0 To j)
pack(0) = cel.Value
j = j + 1
ReDim Preserve pack(0 To j)
pack(j) = packhdr.Offset(cel.Row - firstrow + 1, 0).Value 'put the first found value into the first box
firstfindrow = cel.Row
Set c = Range(NI1hdr.Offset(1, 0), NI1hdr.Offset(lastcel.Row, 0))
Set cel = c.Find(cel.Value, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Do
If cel Is Nothing Then Exit Do
isinarray = False
For k = 1 To j
If packhdr.Offset(cel.Row - firstrow + 1, 0).Value = pack(k) Then isinarray = True 'check if packhdr.value is in NI1(i, j)
Next k
If Not isinarray Then
j = j + 1
ReDim Preserve pack(0 To j)
pack(j) = packhdr.Offset(cel.Row - firstrow + 1, 0).Value
End If
Set cel = c.FindNext(cel) 'find next row with the current NI1
Loop While cel.Row <> firstfindrow
If j > MaxNbPack Then MaxNbPack = j
ReDim Preserve packtmp(0 To i)
packtmp(i) = j
ReDim Preserve NI1(0 To i)
NI1(i) = pack
ReDim Preserve NI1tmp(0 To i)
NI1tmp(i) = NI1(i)(0)
i = i + 1
End If
Next cel
nbNI1 = i - 1
End Sub