Merged cell range

Hi,

I would like a formula or custom vba formula to spit out the beginning and the end of a merged cell row number, any help would be appreciated.

for example, I have A1:A4 merged, and I would like =mergebeg(A1)=1 and =mergeend(A1)=4
WangstaaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

[ fanpages ]IT Services ConsultantCommented:
Hi,

Without any error checking (to see if the passed cell reference is actually within a merged range, for instance):

Public Function mergebeg(ByVal objCell As Range) As Long

  mergebeg = objCell.MergeArea.Row
  
End Function
Public Function mergeend(ByVal objCell As Range) As Long

  mergeend = objCell.MergeArea.Row + objCell.MergeArea.Rows.Count - 1&

End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
You can combine fanpages' code into one UDF like this.

Function MergedRange(objCell As Range, Optional strEnd As String = "")
If UCase(strEnd) = "END" Then
    MergedRange = objCell.MergeArea.Row + objCell.MergeArea.Rows.Count - 1
Else
    MergedRange = objCell.MergeArea.Row
End If
End Function

Open in new window

To get beginning: =MergedRange(A1)
To get end: =MergedRange(A1,"end")
0
Martin LissOlder than dirtCommented:
Actually fanpages' and my modifications above only work for vertically oriented merged ranges. The following works for any size merged range. Note that a #VALUE! error will occur if you try to get the "end" value for a NON-merged range.

Function MergedRange(objCell As Range, Optional strEnd As String = "")
If UCase(strEnd) = "END" Then
    MergedRange = Replace(Split(objCell.MergeArea.Address, ":")(1), "$", "")
Else
    MergedRange = Replace(Split(objCell.MergeArea.Address, ":")(0), "$", "")
End If
End Function

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

[ fanpages ]IT Services ConsultantCommented:
Marty:
"Actually fanpages' and my modifications above only work for vertically oriented merged ranges"

My code works with a single cell (one column/one row), a merged range on one row (i.e. multiple columns), a merged range in one column (i.e. multiple rows), or a merged range on multiple rows & multiple columns.

Also, there was no need to combine the two functions I provided.

The requirement of the question was to provide two functions!
0
Martin LissOlder than dirtCommented:
My code works with a single cell (one column/one row), a merged range on one row (i.e. multiple columns), a merged range in one column (i.e. multiple rows), or a merged range on multiple rows & multiple columns.
Your code gives only the rows. That may be what the user wants, but it's not clear to me that it is.

Also, there was no need to merge the two functions I provided.

The requirement of the question was to provide two functions!
Need? No. I was just providing a possible alternative.
0
[ fanpages ]IT Services ConsultantCommented:
Thanks for closing the question, Wangstaa.

Marty:
The question was clear to me...

"Hi,

I would like a formula or custom vba formula to spit out the beginning and the end of a merged cell row number, any help would be appreciated.

for example, I have A1:A4 merged, and I would like =mergebeg(A1)=1 and =mergeend(A1)=4"

You snagged a split of the points in any respect.

Well done.
0
Martin LissOlder than dirtCommented:
@Wangstaa: fanpages seems to be upset with me so if you like, please 'Request Attention' and ask a moderator to reopen the question so that you can give all the points to him.
0
[ fanpages ]IT Services ConsultantCommented:
Please leave the question closed & do not re-allocate the points.

It is not the first time I have seen this occur, but as long as Martin Liss can see the issue, hopefully he will be mindful of repeating this behavio(u)r in other threads.
0
WangstaaAuthor Commented:
Sorry guys, i didn't realize points were so important...
0
[ fanpages ]IT Services ConsultantCommented:
"Sorry guys, i didn't realize points were so important..."

They are not.

Trampling over a thread with suggestions of little value (& an admission of not reading the question) is an issue, for me, though.

Given Martin's longevity at the site, he should know better.
0
[ fanpages ]IT Services ConsultantCommented:
Thanks thermoduric.

Sorry you were asked to be involved, but I appreciate your input.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.