Hi and welcome to MrExcel
Review the following observations:
1. Do not use reserved words to name macros or variables. MergeCells is a property of the cell object.
2. The value of the VerticalAlignment property is missing
3. Do not use this: GoTo MergeCells , it is not good practice to use the GoTo statement.
I don't really know what the objective is, you should give an example of what you have on your sheet and another example of what you want as a result.
But try the following:
VBA Code:
Sub Merge_Cells_Test()
Dim rng As Range
For Each rng In Selection
If rng.Value = rng.Offset(1, 0).Value And rng.Value <> "" Then
Range(rng, rng.Offset(1, 0)).Merge
Range(rng, rng.Offset(1, 0)).VerticalAlignment = xlCenter
Range(rng, rng.Offset(1, 0)).HorizontalAlignment = xlCenter
End If
Next
End Sub
----------
Note Code Tag:
In future please use code tags when posting code, rather that a picture.
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.
---------
NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here:
XL2BB Add-in
Note that there is also a "
Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
---------