Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Checking for Proper Entry of Array Formulas.
Written by Allen Wyatt (last updated November 13, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Jeffrey's company has a number of reports that use an extensive number of CSE (Ctrl+Shift+Enter) array formulas. When someone forgets to hold Ctrl and Shift when pressing Enter, the resulting formulas do not equal the correct answer. Auditing each cell, looking for the { } brackets is both tedious and time consuming. Jeffrey wonders if there is a quick way to find the "missing brackets" or raise an error flag if Ctrl+Shift+Enter is not pressed when it should be?
There is no intrinsic or formulaic method of doing this in Excel. This means that you need to turn to a solution that is based on a macro. Fortunately, VBA offers several different ways you can approach this problem. One approach is to simply use a formula to make sure that each formula within a selection is actually an array formula.
Sub MakeCSE1() Dim rCell As Range For Each rCell In Selection rCell.FormulaArray = rCell.Formula Next rCell End Sub
This macro assumes that you'll select the cells to be "converted" before actually running the macro. If you prefer, you could define a range of cells (give the range a name) and then run a similar macro that always does its work on that range.
Sub MakeCSE2() Dim rng As Range Dim rCell As Range Dim rArea As Range Set rng = Range("CSERange") For Each rArea In rng.Areas For Each rCell In rArea.Cells If rCell.HasArray = False Then rCell.FormulaArray = rCell.Formula End If Next rCell Next rArea End Sub
This macro looks for a range named CSERange and then checks every cell in the range. If it doesn't contain an array formula, then the formula is converted to an array formula.
Note the use of the HasArray property to check if a cell contains an array formula. This property can actually be helpful in other ways. For instance, you could create a simple user-defined function, such as this:
Function NoCellArray1(rng As Range) As Boolean NoCellArray1 = Not rng.HasArray End Function
This function returns True if the cell being pointed to doesn't contain an array formula. If it does contain one, then False is returned. You could then use this function as the basis for a conditional format. All you need to do is create a format that uses it in this way:
=NoCellArray1(A5)
Since NoCellArray returns True if the cell doesn't contain an array formula, your conditional format could set the color of the cell to red or set some other visible sign that the cell doesn't have the requisite array formula. You could also use the following function to accomplish the same task:
Function NoCellArray2(rng As Range) As Boolean NoCellArray2 = (Evaluate(rng.FormulaArray) <> rng.Value) End Function
An entirely different approach is to add something to your formulas that allows them to easily be recognized as array formulas. For instance, you could add the following to the end of any of your array formulas:
+N("{")
This doesn't affect the computation in any way but can be easily checked to see if it is there. The checking can be done by an event handler, such as the following:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Right(Selection.FormulaArray, 5) = "(""{"")" Then ActiveCell.Select Selection.FormulaArray = ActiveCell.Formula End If End Sub
Note that the handler checks to see if the formula ends with ("{") and, if it does, forces the formula to be treated as an array formula. The great thing about this approach is that you'll never have to press Ctrl+Shift+Enter on the worksheet again—the event handler takes care of it for you. If, at some point, you want to convert the formula back to a regular (non-array) version, simply modify the formula so it doesn't include +N("{").
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (478) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Checking for Proper Entry of Array Formulas.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Looking for a formula that can return the address of a cell containing a text string? Look no further; the solution is in ...
Discover MoreWhen Excel performs a calculation, the results you see in an unformatted cell may cause a bit of concern. This tip ...
Discover MoreWhen you construct a formula and click on a cell in a different workbook, an absolute reference to that cell is placed in ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-01-28 05:49:02
Ionut
Hi, i tried last option :
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Right(Selection.FormulaArray, 5) = "(""{"")" Then
ActiveCell.Select
Selection.FormulaArray = ActiveCell.Formula
End If
End Sub
It works only partially, meaning it does make the formula back to array ONLY after i click on each cell containg this.
Is any option to make it work instantly(without having to click on each cell?
2020-01-02 18:40:32
Dave Bonin
How nice... I see that the website has squished out all of the spaces it thought were redundant in my macro.
Well, if you know VBA, you can add the leading spaces back in to make it look nice.
If not, the code won't look pretty, but it should still work.
2020-01-02 17:43:30
Dave Bonin
Personally, you just described the very reasons I hate array formulas:
1) They are easy to break.
2) Finding a broken array formula can be difficult.
Instead, I really prefer using the SUMPRODUCT() function. Others like using SUMIFS().
That said, I may receive workbooks from others with broken array formulas with a request to fix them. This is the macro I built to help me.
Sub ShowArrayFormulas()
' Show the array formulas entered on the active sheet
' These are cells with formulas entered using Ctrl-Shift-Enter
' Select the cells containing array formulas
' In addition, offer an option to pattern the cells with purple dots
' The purpose is to aid debugging
' Macro created 2011-01-14 by Dave Bonin
Dim Count As Long
Dim ThisCell As Range
Dim Where As String
Dim ErrorText As String
Dim MsgStatus As Long
' Handle errors gracefully
On Error GoTo ErrorHandler1
ErrorText = vbNullString
MsgStatus = vbInformation
Count = 0
Where = ""
For Each ThisCell In ActiveSheet.UsedRange
If ThisCell.HasArray = True Then
Count = Count + 1
Where = Where & ThisCell.Address & ","
End If
Next
If Count > 0 Then
Where = Left(Where, Len(Where) - 1)
Where = Replace(Where, "$", "")
Where = Replace(Where, "$", "") 'Repeated because this function has limits
On Error GoTo ErrorHandler2
ActiveSheet.Range(Where).Select
On Error GoTo ErrorHandler1
End If
If Count > 1 Then
If ErrorText = vbNullString Then
If MsgBox("There are " & Format(Count, "#,##0") & " cells containing" & vbCr & _
"array formulas on this sheet." & vbCr & vbCr & _
"Hit 'Ok' to pattern the cells." & ErrorText, _
vbOKCancel + MsgStatus, "Show Array Formulas") = vbOK Then
Selection.Interior.Pattern = xlGray16
Selection.Interior.PatternColorIndex = 47
End If
Else
MsgBox "There are " & Format(Count, "#,##0") & " cells containing" & vbCr & _
"array formulas on this sheet." & ErrorText, _
vbOK + MsgStatus, "Show Array Formulas"
End If
ElseIf Count = 1 Then
If ErrorText = vbNullString Then
If MsgBox("Just one cell on this sheet" & vbCr & _
"contains an array formula." & vbCr & vbCr & _
"It may be lonely. Go see." & vbCr & vbCr & _
"Hit 'Ok' to pattern the cells." & ErrorText, _
vbOKCancel + MsgStatus, "Show Array Formulas") = vbOK Then
Selection.Interior.Pattern = xlGray16
Selection.Interior.PatternColorIndex = 47
End If
Else
MsgBox "Just one cell on this sheet" & vbCr & _
"contains an array formula." & vbCr & vbCr & _
"It may be lonely. Go see." & ErrorText, _
vbOKOnly + MsgStatus, "Show Array Formulas"
End If
Else
MsgBox "No cells on this sheet" & vbCr & _
"contain an array formula." & ErrorText, _
vbOKOnly + MsgStatus, "Show Array Formulas"
End If
Exit Sub
ErrorHandler1:
ErrorText = vbCr & vbCr & "An error occurred:" & vbCr & Err.Description & "."
MsgStatus = vbExclamation
Resume Next
ErrorHandler2:
ErrorText = vbCr & vbCr & "Could not select or pattern the array cells."
MsgStatus = vbExclamation
Resume Next
End Sub
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments