Table of Contents
Merging cells with the same values in VBA allows for the consolidation of data into a single cell, making it easier to analyze and organize. This process can be achieved by using the “MergeCells” method, which combines adjacent cells with matching values into a single larger cell. This can save time and effort when working with large datasets, as well as providing a cleaner and more concise display of information. By utilizing VBA code, the merging process can be automated and customized to meet specific needs, making it a valuable tool for data manipulation and presentation.
VBA: Merge Cells with the Same Values
You can use the following syntax in VBA to merge cells with the same values in a particular range:
Sub MergeSameCells()
'turn off display alerts while merging
Application.DisplayAlerts = False
'specify range of cells for mergingSet myRange = Range("A1:C13")
'merge all same cells in range
MergeSame:
For Each cell In myRange
If cell.Value = cell.Offset(1, 0).Value And Not IsEmpty(cell) Then
Range(cell, cell.Offset(1, 0)).Merge
cell.VerticalAlignment = xlCenter
GoTo MergeSame
End IfNext'turn display alerts back on
Application.DisplayAlerts = True
End Sub
This particular macro merges cells with the same values in the range A1:C13.
The following example shows how to use this syntax in practice.
Example: Merge Cells with the Same Values in VBA
Suppose we have the following dataset in Excel that contains information about points scored by various basketball players:

Suppose we would like to merge the cells with the same values in consecutive rows.
We can create the following macro to do so:
Sub MergeSameCells()
'turn off display alerts while merging
Application.DisplayAlerts = False
'specify range of cells for mergingSet myRange = Range("A1:C13")
'merge all same cells in range
MergeSame:
For Each cell In myRange
If cell.Value = cell.Offset(1, 0).Value And Not IsEmpty(cell) Then
Range(cell, cell.Offset(1, 0)).Merge
cell.VerticalAlignment = xlCenter
GoTo MergeSame
End IfNext'turn display alerts back on
Application.DisplayAlerts = True
End SubWhen we run this macro, we receive the following output:

Notice that each of the cells that contained the same Conference name and Team names have been merged.
Note that we used the statement cell.VerticalAlignment = xlCenter to specify that the text should be centered vertically in the cells that are merged together.
Cite this article
stats writer (2024). How can I merge cells with the same values in VBA?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-merge-cells-with-the-same-values-in-vba/
stats writer. "How can I merge cells with the same values in VBA?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-merge-cells-with-the-same-values-in-vba/.
stats writer. "How can I merge cells with the same values in VBA?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-merge-cells-with-the-same-values-in-vba/.
stats writer (2024) 'How can I merge cells with the same values in VBA?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-merge-cells-with-the-same-values-in-vba/.
[1] stats writer, "How can I merge cells with the same values in VBA?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I merge cells with the same values in VBA?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
