VBA Merge cells in range

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am trying to merge cells using macro, but it works quite oddly and I cannot pick the mistake in code.


Code:
VBA Code:
Sub testee()

    Application.DisplayAlerts = False

    Dim rng As Range

MergeCells:

    For Each rng In Selection
        
        If rng.Value = rng.Offset(1, 0).Value And rng.Value <> "" Then
            Range(rng, rng.Offset(1, 0)).Merge
            Range(rng, rng.Offset(1, 0)).HorizontalAlignment = xlCenter
            Range(rng, rng.Offset(1, 0)).VerticalAlignment = xlCenter
            
            Range(rng.Offset(0, 1), rng.Offset(1, 1)).Merge
            Range(rng.Offset(0, 1), rng.Offset(1, 1)).HorizontalAlignment = xlCenter
            Range(rng.Offset(0, 1), rng.Offset(1, 1)).VerticalAlignment = xlCenter
            GoTo MergeCells
        End If
Next



End Sub

Sheet (Data in column A and B, result of macro in column E and F)

BarF.xlsx
ABCDEF
1A10A10
2A10
3A10
4A10
5B10B
6B10
7B10
8B10
9B10
10B10
11C15C
12C15
13D5D
Sheet4


I want it to merge cells in column A and B based on value in column A only. So if it merges two cells in column A, then it merges 2 cells in column B as well, but now it merges all cells in column B. I am selecting only data in column A before I run macro.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I think I know the problem. Once you merge column A, it takes the new reference as merged cell. What I mean is, once A1 and A2 are merged, the new rng reference becomes A2. So the rng.Offset(1,1) reference becomes B3. If I were you, I would use Cells(R1,C1) reference style to be more precise and in control:
VBA Code:
Sub testee()
  Application.DisplayAlerts = False
  Dim rng As Range
MergeCells:

    For Each rng In Selection
      If rng.Value = rng.Offset(1, 0).Value And rng.Value <> "" Then
        Range(Cells(rng.Row, 1), Cells(rng.Offset(1, 0).Row, 1)).Merge
        Range(Cells(rng.Row, 2), Cells(rng.Offset(1, 0).Row - 1, 2)).Merge
        Range(rng, rng.Offset(1, 1)).HorizontalAlignment = xlCenter
        Range(rng, rng.Offset(1, 1)).VerticalAlignment = xlCenter
            
      GoTo MergeCells
    End If
  Next
  
  Application.DisplayAlerts = True
End Sub
 
Upvote 0
Hi kra,

you will find problems being raised through MergedCells.

VBA Code:
Sub MrE_1226096_1700510()
' https://www.mrexcel.com/board/threads/vba-merge-cells-in-range.1226096/

Dim rng As Range
Dim lngStart As Long

Application.DisplayAlerts = False
lngStart = 1

For Each rng In Selection
  If rng.Value <> rng.Offset(1, 0).Value And rng.Value <> "" Then
    With Range(Cells(lngStart, "A"), Cells(rng.Row, "A"))
      .Merge
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With
    With Range(Cells(lngStart, "B"), Cells(rng.Row, "B"))
      .Merge
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With
    lngStart = rng.Row + 1
  End If
Next rng

Application.DisplayAlerts = True

End Sub

Ciao,
Holger
 
Upvote 0
Solution
Hi kra,

you will find problems being raised through MergedCells.

VBA Code:
Sub MrE_1226096_1700510()
' https://www.mrexcel.com/board/threads/vba-merge-cells-in-range.1226096/

Dim rng As Range
Dim lngStart As Long

Application.DisplayAlerts = False
lngStart = 1

For Each rng In Selection
  If rng.Value <> rng.Offset(1, 0).Value And rng.Value <> "" Then
    With Range(Cells(lngStart, "A"), Cells(rng.Row, "A"))
      .Merge
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With
    With Range(Cells(lngStart, "B"), Cells(rng.Row, "B"))
      .Merge
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With
    lngStart = rng.Row + 1
  End If
Next rng

Application.DisplayAlerts = True

End Sub

Ciao,
Holger
Thank you Holger, works like a charm!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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