Hi all!
Im having some issues with my code below. It works fine on majority of rows, but towards the end, it's having some issues.
What my code does is this:
- if values of cells (i,j) and cells (i+r,j), basically 1 cell and the cell below it are the same, then they merge and other columns merge with the same rows
- i looped this withe the "r" loop to compare eg cells A1 and A2, and if they merge, then A1 and A3, as i may have multiple lines of the same data
however, towards the last few rows, the cells end up merging more than it should..
for example, if I have different data in A4 and A5, with A5 being the last data, the code will merge A4 AND A5, all the way till A10 or so even though A4 and A5 are different, and A6-A10 have no data.
Any help would be great to solve this merging issue!!
Thank you!!!
[TABLE="width: 1092"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Im having some issues with my code below. It works fine on majority of rows, but towards the end, it's having some issues.
What my code does is this:
- if values of cells (i,j) and cells (i+r,j), basically 1 cell and the cell below it are the same, then they merge and other columns merge with the same rows
- i looped this withe the "r" loop to compare eg cells A1 and A2, and if they merge, then A1 and A3, as i may have multiple lines of the same data
however, towards the last few rows, the cells end up merging more than it should..
for example, if I have different data in A4 and A5, with A5 being the last data, the code will merge A4 AND A5, all the way till A10 or so even though A4 and A5 are different, and A6-A10 have no data.
Any help would be great to solve this merging issue!!
Thank you!!!
[TABLE="width: 1092"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Code:
Dim i As Long Dim j As Long
Dim r As Long
For i = 1 To Range("B" & rows.count).End(xlUp).Row
For j = 2 To 2
For r = 1 To 5
If Cells(i, j).Value = Cells(i + r, j).Value Then
Range(Cells(i, j), Cells(i + r, j)).Merge
Range(Cells(i, j - 1), Cells(i + r, j - 1)).Merge
Range(Cells(i, j + 7), Cells(i + r, j + 7)).Merge
Range(Cells(i, j + 8), Cells(i + r, j + 8)).Merge
Range(Cells(i, j + 9), Cells(i + r, j + 9)).Merge
End If
Next r
Next j
Next i
Columns("A:J").HorizontalAlignment = xlLeft
Columns("A:J").VerticalAlignment = xlCenter