Long piece of code, I hope it is readable.
The code is basically if statements looking at 4 arrays: ArrPnLDataD1, PricingDatesArr, ArrForwardCurves and ArrHistoricalPrices.
I thought that having gone with arrays, the code would run quite fast but due to the size of the arrays and the number of loops it is running for too long. Any suggestions on how to optimize?
For k = LBound(ArrPnLDataD1, 1) To UBound(ArrPnLDataD1, 1)
For j = LBound(PricingDatesArr, 1) To UBound(PricingDatesArr, 1)
If ArrPnLDataD1(k, 158) <> "N/A Bio Element" Then
If ArrPnLDataD1(k, 43) = "Phys - Time Based - Fixed Forward Price" Or ArrPnLDataD1(k, 43) = "Phys - Movement Based - Fixed Forward Price" Then 'If FWD Fixed price case
For n = 2 To UBound(ArrForwardCurves, 2)
If ArrPnLDataD1(k, 77) = ArrForwardCurves(1, n) And PricingDatesArr(j, 4) = ArrForwardCurves(3, n) Then 'Curve 1
For x = 8 To UBound(ArrForwardCurves, 1)
If ArrPnLDataD1(k, 158) = ArrForwardCurves(x, 1) Then
PricingDatesArr(j, 10) = ArrForwardCurves(x, n) 'Populate curve 1 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 83) = ArrForwardCurves(1, n) And PricingDatesArr(j, 4) = ArrForwardCurves(3, n) Then 'Curve 2
For x = 8 To UBound(ArrForwardCurves, 1)
If ArrPnLDataD1(k, 158) = ArrForwardCurves(x, 1) Then
PricingDatesArr(j, 11) = ArrForwardCurves(x, n) 'Populate curve 2 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 89) = ArrForwardCurves(1, n) And PricingDatesArr(j, 4) = ArrForwardCurves(3, n) Then 'Curve 3
For x = 8 To UBound(ArrForwardCurves, 1)
If ArrPnLDataD1(k, 158) = ArrForwardCurves(x, 1) Then
PricingDatesArr(j, 12) = ArrForwardCurves(x, n) 'Populate curve 3 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 95) = ArrForwardCurves(1, n) And PricingDatesArr(j, 4) = ArrForwardCurves(3, n) Then 'Curve 4
For x = 8 To UBound(ArrForwardCurves, 1)
If ArrPnLDataD1(k, 158) = ArrForwardCurves(x, 1) Then
PricingDatesArr(j, 13) = ArrForwardCurves(x, n) 'Populate curve 4 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 101) = ArrForwardCurves(1, n) And PricingDatesArr(j, 4) = ArrForwardCurves(3, n) Then 'Curve 5
For x = 8 To UBound(ArrForwardCurves, 1)
If ArrPnLDataD1(k, 158) = ArrForwardCurves(x, 1) Then
PricingDatesArr(j, 14) = ArrForwardCurves(x, n) 'Populate curve 5 Price
End If
Next x
End If
Next n
Else ' not forward fixed price
If PricingDatesArr(j, 2) > ReportDate Then 'Pricing dates is in the future
For n = 2 To UBound(ArrForwardCurves, 2)
If ArrPnLDataD1(k, 77) = ArrForwardCurves(1, n) And PricingDatesArr(j, 3) = ArrForwardCurves(3, n) Then 'Curve 1
For x = 8 To UBound(ArrForwardCurves, 1)
If PricingDatesArr(j, 2) = ArrForwardCurves(x, 1) Then
PricingDatesArr(j, 10) = ArrForwardCurves(x, n) 'Populate curve 1 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 83) = ArrForwardCurves(1, n) And PricingDatesArr(j, 3) = ArrForwardCurves(3, n) Then 'Curve 2
For x = 8 To UBound(ArrForwardCurves, 1)
If PricingDatesArr(j, 2) = ArrForwardCurves(x, 1) Then
PricingDatesArr(j, 11) = ArrForwardCurves(x, n) 'Populate curve 2 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 89) = ArrForwardCurves(1, n) And PricingDatesArr(j, 3) = ArrForwardCurves(3, n) Then 'Curve 3
For x = 8 To UBound(ArrForwardCurves, 1)
If PricingDatesArr(j, 2) = ArrForwardCurves(x, 1) Then
PricingDatesArr(j, 12) = ArrForwardCurves(x, n) 'Populate curve 3 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 95) = ArrForwardCurves(1, n) And PricingDatesArr(j, 3) = ArrForwardCurves(3, n) Then 'Curve 4
For x = 8 To UBound(ArrForwardCurves, 1)
If PricingDatesArr(j, 2) = ArrForwardCurves(x, 1) Then
PricingDatesArr(j, 13) = ArrForwardCurves(x, n) 'Populate curve 4 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 101) = ArrForwardCurves(1, n) And PricingDatesArr(j, 3) = ArrForwardCurves(3, n) Then 'Curve 5
For x = 8 To UBound(ArrForwardCurves, 1)
If PricingDatesArr(j, 2) = ArrForwardCurves(x, 1) Then
PricingDatesArr(j, 14) = ArrForwardCurves(x, n) 'Populate curve 5 Price
End If
Next x
End If
Next n
ElseIf PricingDatesArr(j, 2) <= ReportDate Then 'Pricing dates is in the past
For n = 2 To UBound(ArrHistoricalPrices, 2)
If ArrPnLDataD1(k, 77) = ArrHistoricalPrices(1, n) Then 'Curve 1
For x = 6 To UBound(ArrHistoricalPrices, 1)
If PricingDatesArr(j, 2) = ArrHistoricalPrices(x, 1) Then
PricingDatesArr(j, 10) = ArrForwardCurves(x, n) 'Populate curve 1 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 83) = ArrHistoricalPrices(1, n) Then 'Curve 2
For x = 6 To UBound(ArrHistoricalPrices, 1)
If PricingDatesArr(j, 2) = ArrHistoricalPrices(x, 1) Then
PricingDatesArr(j, 11) = ArrForwardCurves(x, n) 'Populate curve 2 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 89) = ArrHistoricalPrices(1, n) Then 'Curve 3
For x = 6 To UBound(ArrHistoricalPrices, 1)
If PricingDatesArr(j, 2) = ArrHistoricalPrices(x, 1) Then
PricingDatesArr(j, 12) = ArrForwardCurves(x, n) 'Populate curve 3 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 95) = ArrHistoricalPrices(1, n) Then 'Curve 4
For x = 6 To UBound(ArrHistoricalPrices, 1)
If PricingDatesArr(j, 2) = ArrHistoricalPrices(x, 1) Then
PricingDatesArr(j, 13) = ArrForwardCurves(x, n) 'Populate curve 4 Price
End If
Next x
ElseIf ArrPnLDataD1(k, 101) = ArrHistoricalPrices(1, n) Then 'Curve 5
For x = 6 To UBound(ArrHistoricalPrices, 1)
If PricingDatesArr(j, 2) = ArrHistoricalPrices(x, 1) Then
PricingDatesArr(j, 14) = ArrForwardCurves(x, n) 'Populate curve 5 Price
End If
Next x
End If
Next n
End If
End If
End If
Next j
Next k
ArrPnLDataD1(k, 77),For x = 8 To ...,ArrPnLDataD1(k, 158) <> "N/A Bio Element"I have no idea what 77, 88,158 mean \$\endgroup\$hope[the code]is readableWhy is it double spaced?) \$\endgroup\$