I am trying to create macro to fill blank cells with above values then merge the duplicate values and sum the unique values in column C basis on another cell value Column A & B.I have tried but it's not working as per expectation. It's getting messed up; unable to find out the reason.

Please help to understand the exact issue and way to correct it.

**Data**

[![Data](https://i.stack.imgur.com/XaDmA.png)](https://i.stack.imgur.com/XaDmA.png)

**Result**

[![Result](https://i.stack.imgur.com/WxaAl.png)](https://i.stack.imgur.com/WxaAl.png)

```
Sub merge()

Columns("A:C").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"

Dim i As Long
Dim lr As Long

Dim val As String
Dim total As Long
Application.DisplayAlerts = False
With ActiveSheet
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    val = .Cells(2, 1).Value & .Cells(2, 2).Value
    For i = 2 To lr
        total = .Cells(i, 3).Value
        If .Cells(i, 1).Value & .Cells(i, 2).Value <> val Then
            If .Cells(i, 1).Value = .Cells(i, 1).Offset(-1).MergeArea(1, 1).Value Then
                total = .Cells(i, 3).Offset(-1).MergeArea(1, 1) + .Cells(i, 3).Value
                .Range(.Cells(i, 3), .Cells(i, 3).Offset(-1)).merge
                .Cells(i, 3).MergeArea(1, 1).Value = total
                val = .Cells(i, 1).Value & .Cells(i, 2).Value
            Else
                val = .Cells(i, 1).Value & .Cells(i, 2).Value
            End If
        Else
            If Not i = 2 Then
                .Range(.Cells(i, 1), .Cells(i, 1).Offset(-1)).merge
                .Range(.Cells(i, 2), .Cells(i, 2).Offset(-1)).merge
                .Range(.Cells(i, 3), .Cells(i, 3).Offset(-1)).merge
            End If
        End If
    Next i
End With
Application.DisplayAlerts = True
End Sub

```