Sub abc()
Dim i, LR
LR = Cells(Rows.Count, "BB").End(3).Row
Application.DisplayAlerts = False
On Error Resume Next
For i = LR To 584 Step -1
If Cells(i, 54) = Cells(i - 1, 54) Then
Range(Cells(i, 1), Cells(i - 1, 1)).Merge
End If
Next i
Columns(1).VerticalAlignment = xlCenter
Application.DisplayAlerts = True
End Sub
Cảm ơn bác đã hiểu ý em, em làm được rồi ạ, Bác cho em hỏi thêm 1 chút có lệnh nào cho em random màu. Ví dụ: từ A90-A95 là có giá trị ở Cột BB (BB90-BB95) trùng nhau. Thì tự động đỏ màu Fill Color cho mấy dòng đấy là 1 màu. Tương tự những dòng khác trùng nhau cũng auto cho màu random được không bác?PHP:Sub abc() Dim i, LR LR = Cells(Rows.Count, "BB").End(3).Row Application.DisplayAlerts = False On Error Resume Next For i = LR To 584 Step -1 If Cells(i, 54) = Cells(i - 1, 54) Then Range(Cells(i, 1), Cells(i - 1, 1)).Merge End If Next i Columns(1).VerticalAlignment = xlCenter Application.DisplayAlerts = True End Sub
Sub GPE()
Dim I, lR, MyColor As Byte
lR = Cells(Rows.Count, "BB").End(xlUp).Row
Application.DisplayAlerts = False: Randomize
On Error Resume Next: MyColor = 34 + 9 * Rnd() \ 1
For I = lR To 584 Step -1
If Cells(I, 54) = Cells(I - 1, 54) Then
Range(Cells(I, 1), Cells(I - 1, 1)).Merge
Range(Cells(I, 54), Cells(I - 1, 54)).Interior.ColorIndex = MyColor
Else
MyColor = MyColor + 1: If MyColor > 41 Then MyColor = 34
End If
Next I
Cells(584, "A").Resize(lR - 584).VerticalAlignment = xlCenter 'GPE.COM'
Application.DisplayAlerts = True
End Sub
Dựa theo Code của bác í thôi:
PHP:Sub GPE() Dim I, lR, MyColor As Byte lR = Cells(Rows.Count, "BB").End(xlUp).Row Application.DisplayAlerts = False: Randomize On Error Resume Next: MyColor = 34 + 9 * Rnd() \ 1 For I = lR To 584 Step -1 If Cells(I, 54) = Cells(I - 1, 54) Then Range(Cells(I, 1), Cells(I - 1, 1)).Merge Range(Cells(I, 54), Cells(I - 1, 54)).Interior.ColorIndex = MyColor Else MyColor = MyColor + 1: If MyColor > 41 Then MyColor = 34 End If Next I Cells(584, "A").Resize(lR - 584).VerticalAlignment = xlCenter 'GPE.COM' Application.DisplayAlerts = True End Sub
Nhưng mình khuyên bạn không nên trộn ô như vậy; Không thích hợp khi xài VBA
Dạ, mục đích của em tô màu cho dễ nhìn thôi bác ạ, nên không sao đâu ạ. Thực sự code PHP, em nhìn vào còn hiểu được chút. Cấu trúc và code VBA viết cho Excel này em không hiểu gì luôn. Bác giúp em với ạLợi bất cập hại; Làm như bạn iêu cầu tốn điện lắm, xin can!
Chứ làm thì cũng dễ mà & bạn suy nghĩ đi, đến ngày mai mình sẽ hướng dẫn, nếu bạn chưa làm được!
Range(Cells(I, 54), Cells(I - 1, 54)).Interior.ColorIndex = MyColor
Range(Cells(I, 2), Cells(I - 1, 2)).Resize(,54).Interior.ColorIndex = MyColor