Solved

Merge cells if equal

Posted on 2013-12-02
8
329 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now