So using merged cells make it really annoying to work with a lot of formulas, so I wrote a VBA solution using Arrays:
First this function will go through the range and each time it recognizes a merged cell, the code will add the cells to an Array.
Later when the loop comes to a cell marked as "merged" (= is in the array), the count will skip it (thanks to this topic: Check if a value is in an array or not with Excel VBA).
Option Explicit
Function CountCells(RA As Range) As Long
Application.Volatile
Dim i As Long
Dim a As Long
Dim i2 As Long
Dim a2 As Long
Dim RowCount As Long
Dim ColCount As Long
Dim k As Long
Dim R1 As Long
Dim R2 As Long
Dim C1 As Long
Dim C2 As Long
ReDim iArray(1 To 1) As Variant
R1 = RA.Row
R2 = R1 + RA.Rows.Count - 1
C1 = RA.Column
C2 = C1 + RA.Columns.Count - 1
k = 0
For i = R1 To R2
For a = C1 To C2
If IsInArray(Cells(i, a).Address, iArray) Then
GoTo next_a
End If
ColCount = Cells(i, a).MergeArea.Columns.Count
RowCount = Cells(i, a).MergeArea.Rows.Count
If RowCount > 1 Or ColCount > 1 Then
k = k + RowCount * ColCount - 1
For i2 = i To i + RowCount - 1
For a2 = a To a + ColCount - 1
iArray(UBound(iArray)) = Cells(i2, a2).Address
ReDim Preserve iArray(1 To UBound(iArray) + 1) As Variant
Next a2
Next i2
End If
next_a:
Next a
Next i
CountCells = (R2 + 1 - R1) * (C2 + 1 - C1) - k
End Function
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Application.Volatile
Dim i
For i = LBound(arr) To UBound(arr)
If arr(i) = stringToBeFound Then
IsInArray = True
Exit Function
End If
Next i
IsInArray = False
End Function
Then you just need to use this function like this in your sheet:
=countcells(my_range)
or any other range instead of your range named my_range
Note: With Application.Volatile
the function updates automatically, but only when you update the sheet with numbers but just not directly when you merge or unmerge cells.
& "/" &
just use/
– Scott CranerDIV/0!
error which I've literally never seen before. I didn't know an error could split like that. – user9686961=COUNTA(my_range) /(COUNTA(my_range) + COUNTBLANK(my_range))
– Scott Cranermy_range
refers to? – BigBenCOUNTBLANK
will return a#VALUE!
error ifmy_range
has more than one area. – BigBen