Results 1 to 6 of 6

Merge identical cells in table

Threaded 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. #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

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. [SOLVED] 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