Solved

Merge cells if equal

Posted on 2013-12-02
8
364 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

821 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