Merge of cells

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,115
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have data like this

Fixed FTTH CBU Tech Compliants V1.xlsm
BCD
24Node*+Root CauseTicket Number+
25218-00_SAFAKH00OL0Access-Uplink issuePR00035914963|PR00035915931
26Dark FiberPR00035913520|PR00035914983|PR00035916061|PR00035917254|PR00035920093
27MDTPR00035931421|PR00035931520|PR00035933798|PR00035933799|PR00035933811
28116-42_NFELRDAAOLKMan MadePR00035951924
29111-00_HTYNRD00OL5MDTPR00035909693|PR00035923231
30Trans MediaPR00035963757
31116-42_NFELRDAAOT1Trans MediaPR00035967382
OLT2


and looking for this output using VBA, but no luck till now any idea, as merge of cells in column B will be dynamic depending on column C

output
Fixed FTTH CBU Tech Compliants V1.xlsm
BCD
24Node*+Root CauseTicket Number+
25218-00_SAFAKH00OL0Access-Uplink issuePR00035914963|PR00035915931
26Dark FiberPR00035913520|PR00035914983|PR00035916061|PR00035917254|PR00035920093
27MDTPR00035931421|PR00035931520|PR00035933798|PR00035933799|PR00035933811
28116-42_NFELRDAAOLKMan MadePR00035951924
29111-00_HTYNRD00OL5MDTPR00035909693|PR00035923231
30Trans MediaPR00035963757
31116-42_NFELRDAAOT1Trans MediaPR00035967382
OLT2
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I tried this code but still no luck,

VBA Code:
Sub MergeCellsBasedOnCriteria()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("OLT2") ' Change "Sheet1" to the name of your sheet

    Dim startCell As Range
    Dim endCell As Range
    Dim currentRow As Long
    Dim lastRow As Long
    
    ' Initialize variables
    currentRow = 25
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

    ' Loop through rows starting from B25
    Do While currentRow <= lastRow
        ' Check if the current cell in column B is not blank
        If ws.Cells(currentRow, "B").Value <> "" Then
            ' Set startCell if it's not already set (i.e., start of a new range)
            If startCell Is Nothing Then
                Set startCell = ws.Cells(currentRow, "B")
            End If
        Else
            ' Check if startCell is set and if the corresponding cell in column C is not empty
            If Not startCell Is Nothing And ws.Cells(currentRow, "C").Value <> "" Then
                ' Set endCell
                Set endCell = ws.Cells(currentRow - 1, "B")
                ' Merge the cells from startCell to endCell
                ws.Range(startCell, endCell).Merge
                ' Reset startCell and endCell for the next range
                Set startCell = Nothing
                Set endCell = Nothing
            End If
        End If
        currentRow = currentRow + 1
    Loop
End Sub
 
Upvote 0
Try:
VBA Code:
Sub MergeCells()
    Application.ScreenUpdating = False
    Dim lRow As Long, x As Long, i As Long, rng As Range
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = lRow To 26 Step -1
        If Cells(x - 1, 1) <> "" Then
            Range("A" & x - 1).EntireRow.Insert
        End If
    Next x
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Application.DisplayAlerts = False
    For Each rng In Range("B26:B" & lRow).SpecialCells(xlCellTypeConstants).Areas
        rng.Offset(, -1).Merge
    Next
    Application.DisplayAlerts = True
    Range("B24:B" & lRow).SpecialCells(xlBlanks).EntireRow.Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
@vmjan02 that is just a terrible idea. Once you do that if you then want to apply a filter, do a pivot or any other analysis you won't be able to.
Most of us avoid merged cells like the plague.
 
Upvote 0
Try:
VBA Code:
Sub MergeCells()
    Application.ScreenUpdating = False
    Dim lRow As Long, x As Long, i As Long, rng As Range
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = lRow To 26 Step -1
        If Cells(x - 1, 1) <> "" Then
            Range("A" & x - 1).EntireRow.Insert
        End If
    Next x
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Application.DisplayAlerts = False
    For Each rng In Range("B26:B" & lRow).SpecialCells(xlCellTypeConstants).Areas
        rng.Offset(, -1).Merge
    Next
    Application.DisplayAlerts = True
    Range("B24:B" & lRow).SpecialCells(xlBlanks).EntireRow.Delete
    Application.ScreenUpdating = True
End Sub
the output is not as desired, it not the output i am looking for. I have shared the desired output on my first post.


1720707720084.png
 
Upvote 0
@vmjan02 that is just a terrible idea. Once you do that if you then want to apply a filter, do a pivot or any other analysis you won't be able to.
Most of us avoid merged cells like the plague.
yes i do and well aware about it as well, but its the final stage and this output will be added to the ppt slide, so working out a dynamic merge cell. once this part of pasting the range ot PPT is over, then it will delete all the rows for the next working.
 
Upvote 0
This is the result when I ran the macro below:
Book1
BCD
24Node*+Root CauseTicket Number+
25218-00_SAFAKH00OL0Access-Uplink issuePR00035914963|PR00035915931
26Dark FiberPR00035913520|PR00035914983|PR00035916061|PR00035917254|PR00035920093
27MDTPR00035931421|PR00035931520|PR00035933798|PR00035933799|PR00035933811
28116-42_NFELRDAAOLKMan MadePR00035951924
29111-00_HTYNRD00OL5MDTPR00035909693|PR00035923231
30Trans MediaPR00035963757
31116-42_NFELRDAAOT1Trans MediaPR00035967382
Sheet4

It looks exactly the same as the second table in your original post.
VBA Code:
Sub MergeCells()
    Application.ScreenUpdating = False
    Dim lRow As Long, x As Long, i As Long, rng As Range
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = lRow To 25 Step -1
        If Cells(x, 2) <> "" And Cells(x - 1, 3) <> "" Then
            Range("A" & x).EntireRow.Insert
        End If
    Next x
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Application.DisplayAlerts = False
    For Each rng In Range("C26:C" & lRow).SpecialCells(xlCellTypeConstants).Areas
        rng.Offset(, -1).Merge
    Next
    Application.DisplayAlerts = True
    Range("C24:C" & lRow).SpecialCells(xlBlanks).EntireRow.Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,221,706
Messages
6,161,406
Members
451,703
Latest member
rvan07

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