Sub demo()
Const sFrm As String = _
"=IF(AND(RC[-12]:RC[-1]=""""),""No RoB assessments performed""," & vbLf & _
"IF(OR(CHOOSE({1,2,3,4,5},RC[-10],RC[-8],RC[-6],RC[-4],RC[-2])=""no""),""HIGH""," & vbLf & _
"IF(OR(CHOOSE({1,2,3,4,5},RC[-10],RC[-8],RC[-6],RC[-4],RC[-2])=""High Risk""),""HIGH""," & vbLf & _
"IF(OR(CHOOSE({1,2,3,4,5},RC[-10],RC8],RC[-6],RC[-4],RC[-2])=""UNCLEAR""),""UNCLEAR""," & vbLf & _
"IF(OR(CHOOSE({1,2,3,4,5},RC[-10],RC[-8],RC[-6],RC[-4],RC[-2])=""Unclear Risk""),""UNCLEAR"",""LOW"")))))"
Debug.Print InsertArrayFormula(Range("M1"), sFrm)
End Sub
Function InsertArrayFormula(r As Range, _
sFrm As String, _
iRef As XlReferenceStyle, _
Optional ByVal sFmt As String = "") As Boolean
' shg 2009, 2012
' Inserts the A1 or R1C1 array formula sFrm into r
' The VBE CANNOT have focus when this runs!
Dim iRefSav As XlReferenceStyle ' current ref style
Dim rSel As Range ' current selection
If r.Worksheet.ProtectContents Then Exit Function
Set rSel = ActiveWindow.RangeSelection
With Application
iRefSav = .ReferenceStyle
.ReferenceStyle = iRef
On Error GoTo Oops
.ScreenUpdating = False
With r.Areas(1)
' Can't put an array formula in cells that are
' not either all locked or all unlocked, so ...
.Locked = .Cells(1).Locked
' Cache the number format, set to text, insert formula, restore format
If Len(sFmt) = 0 Then sFmt = .NumberFormat
.NumberFormat = "@"
.Value = sFrm
.NumberFormat = sFmt
Application.Goto .Cells
End With
DoEvents
.SendKeys "{F2}^+~"
DoEvents
.Goto rSel
InsertArrayFormula = True
Outtahere:
.ReferenceStyle = iRefSav
.ScreenUpdating = True
Exit Function
End With
Oops:
Resume Outtahere
End Function
Bookmarks