ChinookDude
New Member
- Joined
- Aug 10, 2010
- Messages
- 3
Hello,
I am trying to get a macro to work but it fails (looks like it is running but does not merge) if there are any blank rows in the spreadsheet at the time the macro is executed. If there are no blank rows the macro executes correctly. I am not good at programming so I have turned to the experts. I know merge and center is the devil, but this is a final "viewing" report. At any time this spreadsheet can have up to 600 rows. Please let me know if anyone needs anymore information.
The merge and center checks each row and if there is consective same cells and then merge and centers the same cells.
Thanks in advance!!
Thanks,
Joey
Here is all of the code that I am using to accomplish this.
I am trying to get a macro to work but it fails (looks like it is running but does not merge) if there are any blank rows in the spreadsheet at the time the macro is executed. If there are no blank rows the macro executes correctly. I am not good at programming so I have turned to the experts. I know merge and center is the devil, but this is a final "viewing" report. At any time this spreadsheet can have up to 600 rows. Please let me know if anyone needs anymore information.
The merge and center checks each row and if there is consective same cells and then merge and centers the same cells.
Thanks in advance!!
Thanks,
Joey
Here is all of the code that I am using to accomplish this.
Code:
'This code merges column 1 for consective same cells
Const iCol As Long = 1 ' pick your column
Const iCol2 As Long = 2 ' pick your column
Const iCol3 As Long = 3 ' pick your column
Const iCol4 As Long = 4 ' pick your column
Dim iRow As Long
Dim jRow As Long
Dim cell As Excel.Range
Dim rMrg As Excel.Range
iRow = 2 ' pick your start row
Application.DisplayAlerts = False
Do While Not IsEmpty(Cells(iRow, iCol))
Set rMrg = Cells(iRow, iCol)
jRow = 1
Do While Cells(iRow + jRow, iCol).Value = Cells(iRow, iCol).Value
Set rMrg = Union(rMrg, Cells(iRow + jRow, iCol))
jRow = jRow + 1
Loop
rMrg.Merge
iRow = iRow + jRow
Loop
iRow = 2 ' pick your start row
Do While Not IsEmpty(Cells(iRow, iCol2))
Set rMrg = Cells(iRow, iCol2)
jRow = 1
Do While Cells(iRow + jRow, iCol2).Value = Cells(iRow, iCol2).Value
Set rMrg = Union(rMrg, Cells(iRow + jRow, iCol2))
jRow = jRow + 1
Loop
rMrg.Merge
iRow = iRow + jRow
Loop
iRow = 2 ' pick your start row
Do While Not IsEmpty(Cells(iRow, iCol3))
Set rMrg = Cells(iRow, iCol3)
jRow = 1
Do While Cells(iRow + jRow, iCol3).Value = Cells(iRow, iCol3).Value
Set rMrg = Union(rMrg, Cells(iRow + jRow, iCol3))
jRow = jRow + 1
Loop
rMrg.Merge
iRow = iRow + jRow
Loop
iRow = 2 ' pick your start row
Do While Not IsEmpty(Cells(iRow, iCol4))
Set rMrg = Cells(iRow, iCol4)
jRow = 1
Do While Cells(iRow + jRow, iCol4).Value = Cells(iRow, iCol4).Value
Set rMrg = Union(rMrg, Cells(iRow + jRow, iCol4))
jRow = jRow + 1
Loop
rMrg.Merge
iRow = iRow + jRow
Loop
Application.DisplayAlerts = True