Hi guys,
hope everyone is well
I was hoping someone could help me? I found a useful macro on here and I've een trying to tweak it but to no avail, and it's driving me nuts! The macro merged cells from the first cell containing a value, all the way down to the last empty cell in columns B & C. I'm trying to change it to complete the same merge for columns B, C & D. I would ideally like this to cover A, B & C but I don't mind having to insert a blank column for the sake of the code. No matter what I change, I can't seem to include the third column!
If whoever is brainy and kind enough to help me, as I am a complete beginner with VBA, would you mind pointing me in the right direction of how to change this?
Code below.
Thank you in advance!
hope everyone is well
I was hoping someone could help me? I found a useful macro on here and I've een trying to tweak it but to no avail, and it's driving me nuts! The macro merged cells from the first cell containing a value, all the way down to the last empty cell in columns B & C. I'm trying to change it to complete the same merge for columns B, C & D. I would ideally like this to cover A, B & C but I don't mind having to insert a blank column for the sake of the code. No matter what I change, I can't seem to include the third column!
If whoever is brainy and kind enough to help me, as I am a complete beginner with VBA, would you mind pointing me in the right direction of how to change this?
Code below.
VBA Code:
With ws
lngrow = Range("D" & .Rows.Count).End(xlUp).Row
Set rng = Range(.Cells(2, 4), .Cells(lngrow, 4))
For Each cell In rng
Debug.Print cell.Value
If cell.Row = 2 Then
Set rngMERGE = cell.Offset(, -1)
intMERGE = cell.Row
Else
If Not cell.Row = lngrow Then
If Not cell.Offset(, -1).Value = "" And intMERGE < cell.Row Then
Set rngMERGE = Range(.Cells(rngMERGE.Row, cell.Offset(, -1) _
.Column), .Cells(cell.Offset(-1).Row, cell. _
Offset(, -1).Column))
Set rngMERGEA = rngMERGE.Offset(, -1)
rngMERGE.Select
With rngMERGE
.MergeCells = True
.VerticalAlignment = xlCenter
End With
rngMERGEA.Select
With rngMERGEA
.MergeCells = True
.VerticalAlignment = xlCenter
End With
Set rngMERGE = cell.Offset(, -1)
intMERGE = cell.Row
End If
Else
If cell.Offset(, -1).Value = "" Then
Set rngMERGE = Range(.Cells(rngMERGE.Row, cell.Offset(, -1) _
.Column), .Cells(cell.Row, cell. _
Offset(, -1).Column))
Set rngMERGEA = rngMERGE.Offset(, -1)
rngMERGE.Select
With rngMERGE
.MergeCells = True
.VerticalAlignment = xlCenter
End With
rngMERGEA.Select
With rngMERGEA
.MergeCells = True
.VerticalAlignment = xlCenter
End With
End If
End If
End If
Next cell
End With
End Sub
Thank you in advance!
Last edited by a moderator: