+ Reply to Thread
Results 1 to 6 of 6

Merge identical cells in table

Hybrid View

YasserKhalil Merge identical cells in table 10-25-2015, 02:36 PM
TMS Re: Merge identical cells in... 10-25-2015, 04:15 PM
YasserKhalil Re: Merge identical cells in... 10-25-2015, 05:32 PM
TMS Re: Merge identical cells in... 10-25-2015, 06:09 PM
LJMetzger Re: Merge identical cells in... 10-25-2015, 08:32 PM
YasserKhalil Re: Merge identical cells in... 10-26-2015, 01:35 AM
  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Merge identical cells in table

    Hello everyone
    I have a sheet named "Basic" ..I need to convert it and merge cells to another table not in the same table ..
    For example : In Sheets("Basic").range("B7:C7") the values are identical (the same so they would be merged
    The merge process to be in the same row (no include column) ... I mean for example B9 and B10 not to be merged although they are the same ...

    Another example: V8 and W8 are to be merged together

    In case there are three identical values just the first two be merged only ...
    for example: G8 - H8 - I8 are the same but just G8 - H8 to be merged and I8 still as it is

    In case there are four identical values : each two to be merged ..not all the cells to be merged

    The desired result should be in the sheet("Merged") not in the same sheet

    Hope it is clear
    Thanks advanced for help
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,121

    Re: Merge identical cells in table

    Try:

    Option Explicit
    
    Sub sMergeTopics()
    
    Dim cell As Range
    Dim lLR As Long, LLC As Long
    Const lFR As Long = 7
    Const lFC As Long = 1
    
    lLR = Cells(Rows.Count, lFC).End(xlUp).Row
    LLC = Cells(lFR, Columns.Count).End(xlToLeft).Column
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each cell In Range(Cells(lFR, lFC), Cells(lLR, LLC))
        If cell.Value = cell.Offset(0, 1) _
        And cell.Offset(0, 1) <> "" _
        Then
            cell.Resize(1, 2).MergeCells = True
        End If
    Next 'cell
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Merge identical cells in table

    That's very wonderful
    Thanks a lot for this special and great gift
    Thank you very much

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,121

    Re: Merge identical cells in table

    You're welcome. Thanks for the rep.

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Merge identical cells in table

    Hi Yasser,

    You always seem to have interesting and challenging questions. Try the attached file which contains the following code:
    Option Explicit
    
    Public Const sSourceWorksheetName = "Basic"
    Public Const sDestinationWorksheetName = "Merged"
    Public Const sDestinationWorksheetTopLeftCell = "A21"
    
    Public Const nSpecialBlueColorIndex = 37
    Public Const nSpecialGrayColorIndex = 15
    
    Sub ClearDestinationArea()
    
      Dim wsDestination As Worksheet
      Dim myBigRange As Range
      Dim myDataRange As Range
    
      'Create the Worksheet Object
      Set wsDestination = Sheets(sDestinationWorksheetName)
    
      'Get the Range on the Destination Sheet
      'Calculate the 'Data Range'
      'Add two rows and move up two rows to get the entire range
      Set myDataRange = wsDestination.Range(sDestinationWorksheetTopLeftCell)
      Set myBigRange = myDataRange.Resize(7, 27)
      myBigRange.UnMerge
      myBigRange.Clear
      Debug.Print myBigRange.Address
      
      
      'Clear Object Pointers
      Set wsDestination = Nothing
      Set myBigRange = Nothing
      Set myDataRange = Nothing
    
    End Sub
    
    
    Sub MergeCellsWithLikeData()
    
      Dim wsDestination As Worksheet
      Dim wsSource As Worksheet
      
      Dim r As Range
      Dim myBigRange As Range
      Dim myDataRange As Range
      Dim myMergedRange As Range
      
      Dim iCount As Long
      Dim iRangeEndRow As Long
      Dim iRangeStartRow As Long
      
      Dim sRange As String
      Dim sValue As String
      Dim sValuePrevious As String
    
    
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      'Initial Processing
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      
      'Disable Screen Updating to improve speed performance and to eliminate screen flicker
      Application.ScreenUpdating = False
    
      'Create the Worksheet Objects
      Set wsSource = Sheets(sSourceWorksheetName)
      Set wsDestination = Sheets(sDestinationWorksheetName)
    
    
      'Find the first occurence of .Sun on the Source Worksheet
      'Find the first occurence of the string
      'NOTE: .Find can not locate '.Sun', so looking for 'Sun' in part of the Cell
      Set r = wsSource.Cells.Find(What:="Sun", _
                          After:=wsSource.Range("A1"), _
                          LookIn:=xlValues, _
                          LookAt:=xlPart, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlNext, _
                          MatchCase:=False, _
                          SearchFormat:=False)
      
      If r Is Nothing Then
        MsgBox "NOTHING DONE.  Could not find data table on Sheet '" & sSourceWorksheetName & "'."
        GoTo MYEXIT
      End If
      
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      'Copy from the 'Source Area' to the 'Destination Area'
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      
      'Calculate the 'Data Range' on the Source Sheet
      'Add two rows and move up two rows to get the entire range
      Set myDataRange = r.CurrentRegion
      Set myBigRange = myDataRange.Resize(myDataRange.Rows.Count + 2)
      Set myBigRange = myBigRange.Offset(-2)
      
      'Get the Start Row and End Row of the Big Range on the Source Sheet
      'Copy the Range to the Destination Sheet
      iRangeStartRow = myBigRange.Row
      iRangeEndRow = iRangeStartRow + myBigRange.Rows.Count - 1
      sRange = iRangeStartRow & ":" & iRangeEndRow
      wsSource.Range(sRange).Copy Destination:=wsDestination.Range(sDestinationWorksheetTopLeftCell)
      
      
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      'Color and Merge Cells in the Destination Area
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      
      'Get the Range on the Destination Sheet
      'Calculate the 'Data Range'
      'Add two rows and move up two rows to get the entire range
      Set myDataRange = wsDestination.Range(sDestinationWorksheetTopLeftCell).Offset(2, 0).CurrentRegion
      Set myBigRange = myDataRange.Resize(myDataRange.Rows.Count + 2)
      Set myBigRange = myBigRange.Offset(-2)
      
      
      'Color each Cell in the First Row 'Special Blue'
      For Each r In myBigRange.Resize(1)
        r.Interior.ColorIndex = nSpecialBlueColorIndex
      Next r
      
      'Color each Cell in the Next Row 'Special Gray'
      For Each r In myBigRange.Resize(1).Offset(1)
        r.Interior.ColorIndex = nSpecialGrayColorIndex
      Next r
      
      'Color every 9th Cell Special Gray (starting at the first cell in each row)
      'Merge adjacent cells if the values are the same (reset after a merge)
      iCount = 0
      For Each r In myDataRange
      
        'Initialize the Counter that counts to 9
        'Get the next value from the 'Destination Data Area'
        iCount = iCount + 1
        sValue = Trim(r.Value)
        
        If iCount = 1 Then
          'Color every 9th Cell Special Gray
          r.Interior.ColorIndex = nSpecialGrayColorIndex
        
        ElseIf sValue = sValuePrevious Then
            'Merge adjacent cells with the same value
            'Clear the value in the rightmost cell to avoid an Excel Warning message
            'Create a 'Merged Range' that includes the two cells (this cell and the cell to the left)
            'Clear the 'Curent Value' to avoid 3 matches in a row
            r.Value = ""
            Set myMergedRange = r.Offset(0, -1).Resize(, 2)
            myMergedRange.Merge
            sValue = ""
          
        End If
        
        'Reset the counter when it reaches 9
        If iCount = 9 Then
          iCount = 0
        End If
          
        'Save the 'Current Value' for future use
        sValuePrevious = sValue
        
      Next r
    
    MYEXIT:
      'Enable Screen Updating
      Application.ScreenUpdating = True
      
      'Clear Object Pointers
      Set wsSource = Nothing
      Set wsDestination = Nothing
      Set r = Nothing
      Set myBigRange = Nothing
      Set myDataRange = Nothing
      Set myMergedRange = Nothing
    End Sub
    Lewis

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Merge identical cells in table

    Mr. Lewis
    It's really fascinating masterpiece
    Thanks a lot for this wonderful solution
    Thank you very much

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 06-11-2014, 10:12 PM
  2. VBA to merge cells with identical content
    By juangar1992 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-27-2013, 04:05 PM
  3. Replies: 3
    Last Post: 06-04-2013, 01:00 PM
  4. Merge identical Labels in Pie chart
    By Ravnoss in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 01-21-2010, 10:54 AM
  5. merge identical fields and generate totals.
    By apeman1977 in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 03-18-2009, 11:30 AM
  6. Can't paste as merge cells aren't identical
    By Muttley in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2006, 11:15 AM
  7. [SOLVED] REQ - Help with merge/delete of duplicate/identical cells
    By Mark in forum Excel General
    Replies: 1
    Last Post: 05-04-2005, 08:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1