I have the below formula which works in excel:
{=IF(SUM(O3:Q3)=0,"no",IF(AND(INDEX(Direction_check!D:D,MATCH(1,(Direction_check!A:A=$O3)*(Direction_check!B:B=$P3),0))-INDEX(Direction_check!H:H,MATCH(1,(Direction_check!E:E=$B3)*(Direction_check!F:F=$C3),0))<=4,INDEX(Direction_check!D:D,MATCH(1,(Direction_check!A:A=$O3)*(Direction_check!B:B=$P3),0))-INDEX(Direction_check!H:H,MATCH(1,(Direction_check!E:E=$B3)*(Direction_check!F:F=$C3),0))>=-4),"no","yes"))}
The problem is when I translate this into VBA, as below, the string exceeds the maximum characters for an array formula.
Selection.FormulaArray = _
"=IF(SUM(RC[-3]:RC[-1])=0,""no"",IF(AND(INDEX(Direction_check!C[-14],MATCH(1,(Direction_check!C[-17]=RC15)*(Direction_check!C[-16]=RC16),0))-INDEX(Direction_check!C[-10],MATCH(1,(Direction_check!C[-13]=RC2)*(Direction_check!C[-12]=RC3),0))<=4,INDEX(Direction_check!C[-14],MATCH(1,(Direction_check!C[-17]=RC15)*(Direction_check!C[-16]=RC16),0))-INDEX(Direction_check!C[-" & _
"10],MATCH(1,(Direction_check!C[-13]=RC2)*(Direction_check!C[-12]=RC3),0))>=-4),""no"",""yes""))" & _
""
As a work around I've tried to split the formula into parts, as below:
Sub Macro1()
Dim FormulaPart1 As String
Dim FormulaPart2a As String
Dim FormulaPart2b As String
Dim FormulaPart2c As String
Dim FormulaPart2d As String
FormulaPart2a = "INDEX(Direction_check!C[-14],MATCH(1,(Direction_check!C[-17]=RC15)*(Direction_check!C[-16]=RC16),0))"
FormulaPart2b = "INDEX(Direction_check!C[-10],MATCH(1,(Direction_check!C[-13]=RC2)*(Direction_check!C[-12]=RC3),0))"
FormulaPart2c = "INDEX(Direction_check!C[-14],MATCH(1,(Direction_check!C[-17]=RC15)*(Direction_check!C[-16]=RC16),0))"
FormulaPart2d = "INDEX(Direction_check!C[-" & "10],MATCH(1,(Direction_check!C[-13]=RC2)*(Direction_check!C[-12]=RC3),0))"
FormulaPart1 = "=IF(SUM(RC[-3]:RC[-1])=0,""no"",IF(AND(""X1""-""X2""<=4,""X3""-""X4"">=-4),""no"",""yes""))"
With ActiveSheet.Range("V3")
Dim Originals As Variant
Dim Replacements As Variant
Dim i As Long
Originals = Array("""X1""", """X2""", """X3""", """X4""")
Replacements = Array(FormulaPart2a, FormulaPart2b, FormulaPart2c, FormulaPart2d)
For i = 0 To 3
.FormulaArray = Replace(FormulaPart1, Originals(i), Replacements(i))
Next
End With
End Sub
This works however it only replaces "X4" in the cell, as each time it loops it starts with FormulaPart1 again. Is there anyway to get it to replace all four "X's" in one step? The trouble is if it isn't completed in one step then the length of the array formula exceeds the maximum number of characters and returns an error.
Any help would be great!
Peter
{=IF(SUM(O3:Q3)=0,"no",IF(AND(INDEX(Direction_check!D:D,MATCH(1,(Direction_check!A:A=$O3)*(Direction_check!B:B=$P3),0))-INDEX(Direction_check!H:H,MATCH(1,(Direction_check!E:E=$B3)*(Direction_check!F:F=$C3),0))<=4,INDEX(Direction_check!D:D,MATCH(1,(Direction_check!A:A=$O3)*(Direction_check!B:B=$P3),0))-INDEX(Direction_check!H:H,MATCH(1,(Direction_check!E:E=$B3)*(Direction_check!F:F=$C3),0))>=-4),"no","yes"))}
The problem is when I translate this into VBA, as below, the string exceeds the maximum characters for an array formula.
Selection.FormulaArray = _
"=IF(SUM(RC[-3]:RC[-1])=0,""no"",IF(AND(INDEX(Direction_check!C[-14],MATCH(1,(Direction_check!C[-17]=RC15)*(Direction_check!C[-16]=RC16),0))-INDEX(Direction_check!C[-10],MATCH(1,(Direction_check!C[-13]=RC2)*(Direction_check!C[-12]=RC3),0))<=4,INDEX(Direction_check!C[-14],MATCH(1,(Direction_check!C[-17]=RC15)*(Direction_check!C[-16]=RC16),0))-INDEX(Direction_check!C[-" & _
"10],MATCH(1,(Direction_check!C[-13]=RC2)*(Direction_check!C[-12]=RC3),0))>=-4),""no"",""yes""))" & _
""
As a work around I've tried to split the formula into parts, as below:
Sub Macro1()
Dim FormulaPart1 As String
Dim FormulaPart2a As String
Dim FormulaPart2b As String
Dim FormulaPart2c As String
Dim FormulaPart2d As String
FormulaPart2a = "INDEX(Direction_check!C[-14],MATCH(1,(Direction_check!C[-17]=RC15)*(Direction_check!C[-16]=RC16),0))"
FormulaPart2b = "INDEX(Direction_check!C[-10],MATCH(1,(Direction_check!C[-13]=RC2)*(Direction_check!C[-12]=RC3),0))"
FormulaPart2c = "INDEX(Direction_check!C[-14],MATCH(1,(Direction_check!C[-17]=RC15)*(Direction_check!C[-16]=RC16),0))"
FormulaPart2d = "INDEX(Direction_check!C[-" & "10],MATCH(1,(Direction_check!C[-13]=RC2)*(Direction_check!C[-12]=RC3),0))"
FormulaPart1 = "=IF(SUM(RC[-3]:RC[-1])=0,""no"",IF(AND(""X1""-""X2""<=4,""X3""-""X4"">=-4),""no"",""yes""))"
With ActiveSheet.Range("V3")
Dim Originals As Variant
Dim Replacements As Variant
Dim i As Long
Originals = Array("""X1""", """X2""", """X3""", """X4""")
Replacements = Array(FormulaPart2a, FormulaPart2b, FormulaPart2c, FormulaPart2d)
For i = 0 To 3
.FormulaArray = Replace(FormulaPart1, Originals(i), Replacements(i))
Next
End With
End Sub
This works however it only replaces "X4" in the cell, as each time it loops it starts with FormulaPart1 again. Is there anyway to get it to replace all four "X's" in one step? The trouble is if it isn't completed in one step then the length of the array formula exceeds the maximum number of characters and returns an error.
Any help would be great!
Peter