Function Res(ByVal Mot As Range) As String
Dim Reg As Object
Dim Tabres() As Variant
Dim cpt As Byte: cpt = 1
Set Reg = CreateObject("vbscript.regexp")
With Reg
.Pattern = "\d{4,5}"
.MultiLine = True: .IgnoreCase = True: .Global = True: 'MsgBox .test(Mot.Value)
If .test(Mot.Value) = False Then Exit Function
If .test(Mot.Value) = True Then
Set Matches = .Execute(Mot.Value)
ReDim Tabres(1 To Matches.Count, 1 To 2)
For Each Match In Matches
Tabres(cpt, 1) = Match.Value
Tabres(cpt, 2) = Match.Length
cpt = cpt + 1
Next Match
End If
End With
For i = LBound(Tabres, 1) To UBound(Tabres, 1)
If Tabres(i, 2) = 4 Then
If Chaine4 = Empty Then Chaine4 = Tabres(i, 1) Else Chaine4 = Chaine4 & " / " & Tabres(i, 1)
ElseIf Tabres(i, 2) = 5 Then
If Chaine5 = Empty Then Chaine5 = Tabres(i, 1) Else Chaine5 = Chaine5 & " / " & Tabres(i, 1)
End If
Next i
If Cells(3, ActiveCell.Column) = "NCA" Then
Res = Chaine4
ElseIf Cells(3, ActiveCell.Column) = "NCR" Then
Res = Chaine5
End If
End Function