1 pièce(s) jointe(s)
Loop dans une loop problème
Bonjour,
J'ai une problème sur la premiere loop. Le but du code est de trouver le best match entre deux critères. Sur mon code c'est ratio 1 et ratio 2 et de reporter la volatilité de la colonne C a G.
Le code marche en lui meme, c'est que je n'arrive pas a le faire marcher en loop pour chaque ligne (colonne E et F). mon code ne reporte que la premier volatilité et la met pour les autres lignes. Voir photo:
Pièce jointe 193577
Voici le code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| Sub CheckCell()
Dim resultCell As Double 'fixed this to return string value of Company X
Dim CheckCell As Double
Dim checkCell2 As Double
Dim bestDiff As Double
Dim bestDiff2 As Double
Dim bestDiff3 As Double
Dim dLowValue As Double 'added as a variable to check for lowest value
bestDiff = CheckCell
bestDiff2 = checkCell2
dLowValue = 1000 'set to arbitrary beginning number to test against
For j = 2 To Range("F" & Rows.Count).End(xlUp).Row
CheckCell = Range("E" & j).Value
checkCell2 = Range("F" & j).Value
For i = 2 To Range("C" & Rows.Count).End(xlUp).Row
If (Range("A" & i).Value <= CheckCell Or Range("A" & i).Value >= CheckCell) And (Range("B" & i).Value <= checkCell2 Or Range("B" & i).Value >= checkCell2) Then
If (CheckCell - Range("A" & i).Value) <= bestDiff Or (CheckCell - Range("A" & i).Value) >= bestDiff And (checkCell2 - Range("B" & i).Value) <= bestDiff2 Or (checkCell2 - Range("B" & i).Value) >= bestDiff2 Then
bestDiff3 = Application.WorksheetFunction.Min(Abs(CheckCell - Range("A" & i)) + Abs(checkCell2 - Range("B" & i)))
If bestDiff3 < dLowValue Then
dLowValue = bestDiff3
resultCell = Range("C" & i)
End If
End If
End If
Next i
Range("G" & j).Value = resultCell
Next j
End Sub |
Merci