Need VBA code to merge rows of same entry within a column

Akash Yadav

New Member
Joined
Oct 31, 2020
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
I need to merge rows with similar entries within a column. One important thing to notice is that I do not want to merge 2 row cells if their row cell to their left are different. For example in this example : There is a break in merging of As when entries on their left are different i.e B and C. Thanks in advance.
 

Attachments

  • before.PNG
    before.PNG
    1.2 KB · Views: 8
  • after.PNG
    after.PNG
    1.1 KB · Views: 7

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Akash let's get the ball rolling. Now the solution is only as good as the information we get. At least this will start the conversation. This might generate more questions then answers. But we have to start some place.


VBA Code:
Sub Move1()

Dim CellCnt As Long
Dim row1 As Long
Dim row2 As Long

row2 = 1

CellCnt = Cells(Rows.Count, 1).End(xlUp).Row

For row1 = 1 To CellCnt
If Cells(row1, 2) <> Cells(row1 + 1, 2) Then

Cells(row2, 4) = Cells(row1, 1)
Cells(row2, 5) = Cells(row1, 2)
Cells(row2, 6) = Cells(row1, 3)
row2 = row2 + 1

End If

Next row1

End Sub


Book1
ABCDEF
11BA1BA
21BA1CA
31BA2DA
41CA
51CA
61CA
72DA
82DA
92DA
102DA
Sheet1 (5)
 
Upvote 0
Merge Equal Cells in Column Range

VBA Code:
Option Explicit

Sub mergeEqualCells()
    
    ' Define constants.
    Const wsName As String = "Sheet1"
    Const FirstCell As String = "A1"
    Const Offs As Long = 2
    ' Define workbook.
    Dim wb As Workbook
    Set wb = ThisWorkbook
    
    ' Define Source Column Range.
       
    ' Define worksheet
    Dim ws As Worksheet
    Set ws = wb.Worksheets(wsName)
    ' Define Source Column Range.
    Dim rng As Range
    Set rng = ws.Range(FirstCell)
    Dim Col As Long
    Col = rng.Column
    Dim FirstRow As Long
    FirstRow = rng.Row
    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, Col).End(xlUp).Row
    If LastRow < FirstRow Then
        GoTo ProcExit
    End If
    Set rng = ws.Columns(rng.Column).Resize(LastRow)
    
    ' Write values from Source Column Range to Data Array.
    
    Dim Data As Variant
    If rng.Cells.Count > 1 Then
        Data = rng.Value
    Else
        ReDim Data(1 To 1, 1 To 1)
        Data(1, 1) = rng.Value
    End If
    
    ' Prepare to write from Data Array to Target Column Range.
    
    Dim First As Long
    First = FirstRow
    Dim CurVal As Variant
    CurVal = ws.Cells(First, Col).Value
    Dim CurSize As Long
    CurSize = 1
    Dim i As Long
    
    Application.ScreenUpdating = False
    
    With rng.Offset(, Offs)
        .Clear
        ' Or something like the following combination.
        '.UnMerge
        '.ClearContents
    End With
    
    ' Write from Data Array to Target Column Range.
    
    For i = FirstRow + 1 To LastRow
        If Data(i, 1) <> Data(i - 1, 1) Then
            With ws.Cells(First, Col + Offs)
                .Value = CurVal
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                If CurSize > 1 Then
                    With .Resize(CurSize)
                        .Merge
                    End With
                End If
            End With
            First = i
            CurVal = ws.Cells(First, Col).Value
            CurSize = 1
        Else
            CurSize = CurSize + 1
        End If
    Next i
    With ws.Cells(First, Col + Offs)
        .Value = CurVal
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        If CurSize > 1 Then
            With .Resize(CurSize)
                .Merge
            End With
        End If
    End With
    
    Application.ScreenUpdating = True
    
    ' Inform user.
    
    MsgBox "Cells merged.", vbInformation, "Success"
    
ProcExit:
End Sub
 
Upvote 0
Welcome to the MrExcel board!

Is this what you mean? Test with a copy of your data.

VBA Code:
Sub Merge_Cells()
  Dim r As Long, rws As Long
  
  Application.DisplayAlerts = False
  r = 1
  Do Until r > Range("B" & Rows.Count).End(xlUp).Row
    r = r + 1
    If Cells(r, 2).Value = Cells(r - 1, 2).Value And Cells(r, 1).Value = Cells(r - 1, 1).Value Then
      rws = rws + 1
    Else
      If rws > 1 Then
        With Cells(r - rws, 2).Resize(rws)
          .Merge
          .VerticalAlignment = xlCenter
          .HorizontalAlignment = xlCenter
        End With
      End If
      rws = 1
    End If
  Loop
  Application.DisplayAlerts = True
End Sub

Before:

Akash Yadav.xlsm
ABC
1
21BA
31BA
41BA
51CA
61CA
72DA
82DA
Sheet1


After:

Akash Yadav.xlsm
ABC
1
21BA
31A
41A
51CA
61A
72DA
82A
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,666
Messages
6,120,806
Members
448,990
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top