Merge cells based on the adjacent column.

yeikov

New Member
Joined
Mar 14, 2025
Messages
5
Office Version
  1. 2024
Platform
  1. Windows
Hello Mr. Excel.
I have the following case. Could someone help me with some VBA code?

Merge cells based on the adjacent column.
I've attached the following image.
 

Attachments

  • Captura de pantalla 2025-03-14 022409.png
    Captura de pantalla 2025-03-14 022409.png
    10.8 KB · Views: 15
Hello and welcome to the forum!

Some questions:
Does the cell merge start when the letter "A" is in column B?
If the cell in column B is empty, then there will be no cell merge?
Should the macro put the numbering in column A?
Does cell B1 always start with "A"?
Could you answer all the questions?

1741961649581.png


🤗
 
Upvote 0
If the above is correct.
Try this:

VBA Code:
Sub merging_cells()
  Dim i As Long, n As Long, ini As Long, fin As Long
  Application.ScreenUpdating = False
  Range("A:A").MergeCells = False
  Range("A:A").ClearContents
  ini = 1:  fin = 1
  For i = 1 To Range("B" & Rows.Count).End(3).Row + 1
    If Range("B" & i).Value = "A" Or Range("B" & i).Value = "" Then
      If i > 1 Then
        With Range("A" & ini & ":A" & fin)
          .Merge
          .HorizontalAlignment = xlCenter
          .VerticalAlignment = xlCenter
          n = n + 1
          .Value = n
          ini = i:  fin = i
        End With
      End If
    Else
      fin = i
    End If
  Next
  Application.ScreenUpdating = True
End Sub

🧙‍♂️
 
Last edited:
Upvote 0
Hello Dante
Good evening!
Thank you so much for your tremendous help.
 
Upvote 0
Hi Dante.

If I wanted to start the macro in row 3 and end it at the last value in column "C," what parameters would I need to change?

Saludos!
 
Upvote 0
and end it at the last value in column "C,"
That means that data "A", "B", "C"... are in column C?
Then, change "B" by "C", or by the column with data. 1 by 3:

Rich (BB code):
Sub merging_cells()
  Dim i As Long, n As Long, ini As Long, fin As Long
  Application.ScreenUpdating = False
  Range("A:A").MergeCells = False
  Range("A:A").ClearContents
  ini = 3: fin = 3
  For i = 3 To Range("C" & Rows.Count).End(3).Row + 1
    If Range("C" & i).Value = "A" Or Range("C" & i).Value = "" Then
      If i > 3 Then
        With Range("A" & ini & ":A" & fin)
          .Merge
          .HorizontalAlignment = xlCenter
          .VerticalAlignment = xlCenter
          n = n + 1
          .Value = n
          ini = i: fin = i
        End With
      End If
    Else
      fin = i
    End If
  Next
  Application.ScreenUpdating = True
End Sub


The cells to be merged will remain in column A.

🫡
 
Last edited:
Upvote 0
Hola Dante!
The data "A", "B", "C"... are in column B, but the current macro's numbering extends to the last empty cell. The last data is not numbered.

So I thought the macro could reference the last cell with data so it could be numbered. Column C is where it displays the last record.

Sorry for the language, I'm using Google Translate.
 
Upvote 0
But the image you posted in the original post isn't from Google; you should include a real one, if you don't include a real one, I assume you're responsible for adapting the code to your needs.
In the future, if you can't adapt the code, try to include a real image. That way, the code will be ready for your spreadsheet.

And since you still haven't provided a real image, I'll again assume the last row is column C, but the data starts in row 3:
VBA Code:
Sub merging_cells()
  Dim i As Long, n As Long, ini As Long, fin As Long
  Application.ScreenUpdating = False
  Range("A:A").MergeCells = False
  Range("A:A").ClearContents
  ini = 3: fin = 3
  For i = 3 To Range("C" & Rows.Count).End(3).Row + 1
    If Range("B" & i).Value = "A" Or Range("B" & i).Value = "" Then
      If i > 3 Then
        With Range("A" & ini & ":A" & fin)
          .Merge
          .HorizontalAlignment = xlCenter
          .VerticalAlignment = xlCenter
          n = n + 1
          .Value = n
          ini = i: fin = i
        End With
      End If
    Else
      fin = i
    End If
  Next
  Application.ScreenUpdating = True
End Sub

🫡
 
Upvote 0
Hi Dante!

Thanks for the new changes; now I understand the "macro" better.
Also, thanks for the comment, as I haven't shared the real information. I don't have it yet, either.

Best regards.
 
Upvote 0

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