Hi,
I found the following function so I can count the merged cells in a specified range. When I use the function in the sheet e.g. =COUNTMERGED("A1:Z1") the returned count of merged cells is correct. When I use this formula in another macro (e.g. Range("G8").Formula = "=COUNTMERGED("A1:Z1") and copy to another range of cells the returned value is always #VALUE!
Any ideas why the formula is not resolving when entered as a formula in a range within a macro? Thanks in advance for your help.
I found the following function so I can count the merged cells in a specified range. When I use the function in the sheet e.g. =COUNTMERGED("A1:Z1") the returned count of merged cells is correct. When I use this formula in another macro (e.g. Range("G8").Formula = "=COUNTMERGED("A1:Z1") and copy to another range of cells the returned value is always #VALUE!
Any ideas why the formula is not resolving when entered as a formula in a range within a macro? Thanks in advance for your help.
VBA Code:
Function COUNTMERGED(pWorkRng As Range) As Variant
Dim rng As Range
Set dt = CreateObject("Scripting.Dictionary")
For Each rng In pWorkRng
If rng.MergeCells Then
TempAddress = rng.MergeArea.Address
dt(TempAddress) = ""
End If
Next
COUNTMERGED = dt.Count
End Function