Hi,
There may be better solution...
Sub kTest()
Dim a, i As Long, v, x, y
With ActiveSheet
a = .Range("b3:b" & .Range("b" & Rows.Count).End(xlUp).Row)
End With
v = UNIQUE(a)
For i = 1 To UBound(a, 1)
If Not IsEmpty(a(i, 1)) Then
y = Application.Match(a(i, 1), Application.Index(v, 0, 1), 0)
x = Application.Index(v, y, 2)
CreateBorder Cells(i + 2, "a").Resize(x * 2 - 2 + 1, 13)
i = i + (x * 2 - 2 + 1)
End If
Next
End Sub
Function UNIQUE(v)
Dim i, w(), n As Long, r()
ReDim w(1 To UBound(v, 1), 1 To 2)
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each i In v
If Not IsEmpty(i) Then
If Not .exists(i) Then
n = n + 1: w(n, 1) = i: w(n, 2) = 1
.Add i, Array(n, 2)
Else
r = .Item(i)
w(r(0), 2) = w(r(0), 2) + 1
.Item(i) = r
End If
End If
Next
End With
If n > 0 Then UNIQUE = w
End Function
Sub CreateBorder(ByRef r As Range)
With r
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
End Sub
HTH
Bookmarks