Solved

Merge cells if equal

Posted on 2013-12-02
8
375 Views
Last Modified: 2013-12-02
Hi,
Is it possible to write code or enter a formula that would look down column A and merge the cells that were equal? (there are no empty cells)
0
Comment
Question by:Stephen Byrom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 23

Expert Comment

by:NBVC
ID: 39690951
Do you mean you want to end up with Unique Values Only?

If so,try selecting the column of values and go to Data|Remove Duplicates and click Ok.
0
 
LVL 5

Expert Comment

by:jboddy
ID: 39690995
Can you clarify what you mean by "merge"?
0
 
LVL 1

Author Comment

by:Stephen Byrom
ID: 39691006
Thanks for the pointer, but that contracts the range.
I also tried it using vba with
ActiveSheet.Range("A4:A500").RemoveDuplicates
but that did the same.
I just wanted to clear the cells that were the same. It would be an added bonus if after removing the duplicates the cells could then be merged.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Stephen Byrom
ID: 39691025
by merge I mean that if for example cells a4 to a10 are the same then a4 to a10 would be merged then continue on down to A500 and merging duplicate cells.
a4 to a10 may be the same so merge, a11 to a26 may be the same so merge, etc.
Sorry I am about to embark on a new workbook and the idea came to me to try this.
column A would have numbers lots of them would be the same as they are populated from input from a userform. Columns B through I would also be populated through that form but all the data would be different so I wanted to keep those rows and just merge column A.
0
 
LVL 23

Assisted Solution

by:NBVC
NBVC earned 50 total points
ID: 39691044
You can use Conditional formatting so that the duplicates are coloured as per the background (i.e. white font) so that they are not visible.  This is better than merging cells as that is not recommended, especially if you are going to be referencing them with formulas.

To use conditional formatting, select the range, say A4:A500, then go to Home|Conditonal formatting, then select "use a formula to determine which cells to format", then enter formula:

=COUNTIF($A$4:$A4,$A4)>1

then click Format and choose Font tab to change font to white.

Click Ok and again to finish.
0
 

Assisted Solution

by:Krukanens
Krukanens earned 150 total points
ID: 39691049
'Try this one:

Sub Merge()

    Dim sPreviousField As String
    Dim i As Integer
   
    i = 1
    While Not IsEmpty(Range("A" & i).Value)
       
        If i <> 1 And Range("A" & i).Value = sPreviousField Then
            Application.DisplayAlerts = False
            Range("A" & (i - 1) & ":A" & i).Merge
            Application.DisplayAlerts = True
        Else
            sPreviousField = Range("A" & i).Value
        End If
       
        i = i + 1
    Wend
   
End Sub
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 300 total points
ID: 39691057
Try this

Sub mergesame()
Dim srw As Long
Dim erw As Long
    Application.DisplayAlerts = False
    srw = 1
    Do While Cells(srw, 1) <> ""
        erw = srw
        Do While Cells(srw, 1) = Cells(erw, 1)
            erw = erw + 1
        Loop
        erw = erw - 1
        Range(Cells(srw, 1), Cells(erw, 1)).Merge
        srw = erw + 1
    Loop
    Application.DisplayAlerts = True
End Sub
0
 
LVL 1

Author Closing Comment

by:Stephen Byrom
ID: 39691097
Wow!
Thanks for all the positive solutions.
They all do the trick but ssaqibh gets the lion's share as it executes really quick.

Thanks to all for your time
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question