View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave D-C[_3_] Dave D-C[_3_] is offline
external usenet poster
 
Posts: 176
Default if for merged cells

' This sub may be a start.
' It checks each cell in UsedRange.
' If C2:C4 are merged, it gets a hit
' on the merged cells 3 times.
' But it answers your example,
' giving =SUM($D$2:$D$4).

Sub Sub1()
Dim zCell As Range, iRowA&, iRowZ&, iCol%
For Each zCell In ActiveSheet.UsedRange
zCell.Select ' just to view
If zCell.MergeCells Then
' ck for merge type
If zCell.MergeArea.Columns.Count < 1 Then Stop ' ng
iRowA = zCell.Row
iRowZ = iRowA + zCell.MergeArea.Rows.Count - 1
iCol = zCell.Column + 1
zCell.Formula = "=sum(" & _
Cells(iRowA, iCol).Address & _
":" & _
Cells(iRowZ, iCol).Address & _
")"
End If
Next zCell
End Sub ' D-C Dave

SteveDB1 wrote:
Hi all.
I want to make an if function to look for merged cells, and then add the
contents of another cell related to the merged cells.
I.e.,
if( c2:c_n is merged)
sum(d2:d_n)

where _n is the last cell of the merged/summed group.
How can I perform this function?
part of me is thinking a UDF, but since I'm not entirely familiar with them,
I would appreciate some direction.
Hope everyone has a great T-day weekend.
Thanks.