الكود التالي ينفذ ما تريد وزيادة:
Private Sub Worksheet_Change(ByVal Target As Range)
TC = Target.Column
TR = Target.Row
If TC = 3 And TR > 1 And TR < 31 Then
Set MyRange = [E2:E30]
Set MyRange2 = [C2:C30]
Application.ScreenUpdating = False
With MyRange
.ClearContents
.Interior.ColorIndex = xlNone
End With
For C = 2 To 30
With Cells(C, 5)
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
End With
Next
For R = 2 To 30
If Application.WorksheetFunction.CountIf(MyRange2, Cells(R, 3)) > 1 Then
With Columns(5).Rows(65536).End(xlUp)
.Offset(1, 0) = Cells(R, 3)
End With
End If
Next
For Each Cell In MyRange
If Application.WorksheetFunction.CountIf(MyRange, Cell) > 1 Then
Cell.ClearContents
End If
Next
MyRange.Sort [E2], xlAscending
For R = 2 To 30
If Cells(R, 3).Row Mod 2 = 0 Then Cells(R, 3).Interior.ColorIndex = 35
If Cells(R, 3).Row Mod 2 = 1 Then Cells(R, 3).Interior.ColorIndex = 37
Next
For C = 1 To 15
For Each Cell In MyRange2
If Cell = Cells(C, 5) And Cells(C, 5) <> "" Then
Cell.Interior.ColorIndex = C
Cells(C, 5).Interior.ColorIndex = C
With Cells(C, 5)
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
End With
End If
Next
Next
Application.ScreenUpdating = True
End If
End Sub
شاهد المرفق،
________________.rar