Using Range.Find with xlPrevious should wrap around the worksheet row to find the last occurrence of a value.
Option Explicit
Sub mergeSame()
Dim r As Long, c As Long, c2 As Long
r = 3 'row with 'Year'
c = 1 'column with 'Year'
With Worksheets("sheet3")
Do While Not IsEmpty(.Cells(r, c))
c2 = .Rows(r).Cells.Find(What:=.Cells(r, c).Value, After:=.Cells(r, c), _
MatchCase:=False, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
If c2 > c Then
With .Cells(r, c).Resize(2, 1)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
End With
With .Range(.Cells(r, c), .Cells(r, c2))
Application.DisplayAlerts = False
.Offset(1, 0).Merge
.Merge
Application.DisplayAlerts = True
End With
End If
c = c2 + 1
Loop
End With
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…