Em đã nhờ bạn viết đoạn macro để giải quyết được vấn đề rùi. Đầu tiên dùng Pivot tạo thành 1 cột mã vật tư duy nhất, sau đó chạy macro như sau, tuy hơi chậm 1 chút :
Option Explicit
Private Const SCotMVT = 1, SCotNhomThietBi = 9, Sdongdau = 4
Private Const DCotMVT = 1, DCotNhomThietBi = 2, Ddongdau = 6
Private Type KM
Ma As String
Dong As Integer
End Type
Private S As Integer, D As Integer
Private M(0 To 50000) As KM, P As Integer, L As Integer
Private X As String, NTB As String
Private Sub pTaoM()
D = Ddongdau
L = 0
Do While bTH.Cells(D, DCotMVT).Value <> ""
L = L + 1
With M(L)
.Ma = bTH.Cells(D, DCotMVT).Value
.Dong = D
End With
bTH.Cells(D, DCotNhomThietBi).Value = ""
D = D + 1
Loop
End Sub
Public Sub pTongHop()
pTaoM
S = Sdongdau
X = bVolvo.Cells(S, SCotMVT).Value
If X = "" Then Exit Sub
Do
For P = 1 To L
If X = M(P).Ma Then
NTB = bVolvo.Cells(S, SCotNhomThietBi).Value
If InStr(bTH.Cells(M(P).Dong, DCotNhomThietBi).Value, NTB) = 0 Then
bTH.Cells(M(P).Dong, DCotNhomThietBi).Value = bTH.Cells(M(P).Dong, DCotNhomThietBi).Value & ", " & NTB
End If
Exit For
End If
Next
S = S + 1
X = bVolvo.Cells(S, SCotMVT).Value
Loop Until X = ""
pChuan
End Sub
Private Sub pChuan()
D = Ddongdau
Do While bTH.Cells(D, DCotMVT).Value <> ""
bTH.Cells(D, DCotNhomThietBi).Value = Mid$(bTH.Cells(D, DCotNhomThietBi).Value, 3)
D = D + 1
Loop
End Sub