Bonjour,
Pourquoi la fonction marche quand appelée dans un sub mais pas dans le worksheet ?
Supposons dans une feuille :
A1 = Toto
A2 = Toto
A3 = Toto
A4 = Toto
B1 = 1
B2 = 2
B3 = 3
B4 = 4
Et n'importe ou
=recherchevaleatoire(A1;A1:B4;1)
J'ai l'erreur #Valeur... Mais je ne comprends pas pourquoi...
Function recherchevaleatoire(r1 As Range, r2 As Range, i As Integer) As Variant
Dim tablo() As Variant, aleat As Integer
Application.Volatile True
Set x = r2.Find(r1.Value)
If Not x Is Nothing Then
firstAddress = x.Address
Do
ReDim Preserve tablo(j)
tablo(j) = x.Offset(0, i)
Set x = r2.FindNext(x)
j = j + 1
Loop While Not x Is Nothing And x.Address <> firstAddress
End If
aleat = Application.WorksheetFunction.RandBetween(LBound(tablo), UBound(tablo))
recherchevaleatoire = tablo(aleat)
End Function
Sub toto()
y = recherchevaleatoire(Sheets("Feuil1").Range("A1"), Sheets("Feuil1").Range("A1:B4"), 1)
MsgBox (y)
End Sub
Merci
E