Merge Cells in a Column

Poor Dave

New Member
Joined
Feb 4, 2023
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Good afternoon all. Having trouble making this merge of cells work. Need to limit my merge to a specific column and my data in the column changes daily. The macro is merging the cell values as they repeat, but it is bugging out after all the merges are done on If cell.Value = cell.Offset(1, 0).Value And cell.Value <> “” Then. Is it due to the Range("K:K") code going to blank cells and it should be limited to the end of the column data? Or something else. I thought the And cell.Value <> “” Then would satisfy that. I appreciate your help.


Sub test()

'Merge similar cells

Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set myRange = Range("K:K")
Check:
For Each cell In myRange
If cell.Value = cell.Offset(1, 0).Value And cell.Value <> “” Then
Range(cell, cell.Offset(1, 0)).Merge
cell.VerticalAlignment = xlCenter
GoTo Check
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You can get the last row in column K, e.g.
VBA Code:
Dim lastrow as Long
lastrow = Range("K" & Rows.Count).End(xlUp).Row

Set myRange = Range("K1:K" & lastrow)
 
Upvote 0
Solution
Can mu
You can get the last row in column K, e.g.
VBA Code:
Dim lastrow as Long
lastrow = Range("K" & Rows.Count).End(xlUp).Row

Set myRange = Range("K1:K" & lastrow)
Can multiple columns be 'ranged' at once to merge say column "S", "Z", etc. without rewriting this whole code set one after the other and just changing the column letter.
Thank you.
 
Upvote 0
If you want all the ranges to stop at the same 'lastrow', sure.
VBA Code:
Set myRng = Range("K1:K" & lastrow & ", S1:S" & lastrow & ", Z1:Z" & lastrow)
If you wanted to have different sized ranges (the last row in each column is different) you'd just need to determine the last row in each and perhaps assign each to its own variable, e.g. lastrowK, lastrowS and lastrowZ, then use those in your Set myRng statement where appropriate.
 
Upvote 0
If you want all the ranges to stop at the same 'lastrow', sure.
VBA Code:
Set myRng = Range("K1:K" & lastrow & ", S1:S" & lastrow & ", Z1:Z" & lastrow)
If you wanted to have different sized ranges (the last row in each column is different) you'd just need to determine the last row in each and perhaps assign each to its own variable, e.g. lastrowK, lastrowS and lastrowZ, then use those in your Set myRng statement where appropriate.
Thanks Z. Thought it could be done but wasn't sure about the string language.

Dave
 
Upvote 0
@Poor Dave
Merging cells can create a lot of issues if you are using number data or need to further use VBA to do other tasks.
Without seeing your data, it would be wise to avoid merged cells whenever possible. Merged cells are the nightmare of VBA and Excel, should never have been a feature in Excel
 
Upvote 0
@Poor Dave
Merging cells can create a lot of issues if you are using number data or need to further use VBA to do other tasks.
Without seeing your data, it would be wise to avoid merged cells whenever possible. Merged cells are the nightmare of VBA and Excel, should never have been a feature in Excel
Thanks Mr Mike. I had read that in several of these messages to avoid where possible. Thankfully in my code I am merging after all other code is run so the printout doesn't have repeated data in the column.

Dave
 
Upvote 0
Ok....so long as you are aware of the possible issues...(y):cool:
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

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