A HOANG 620
Thành viên mới

- Tham gia
- 16/1/23
- Bài viết
- 30
- Được thích
- 3
em nhờ anh chị, chú bác gom giúp em đoạn code cho ngắn gọn dưới đây và khắc phục lỗi ở dòng 24, 25, 26 với ạ. em cám ơn
Sub ABC()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim Dic As Object
Dim Nguon(), Kq(), Key, ViTri, SoLan
Dim Dong, Irow, a As Long
Dim Tmr As Double, j As Long, W As Integer, Rws As Long
Dim arr(), aDL()
Set Dic = CreateObject("Scripting.Dictionary")
'------------------------------------------------------------------------------------------------------------------------------
'phan tinh cot A, cot B, cot C, cot E, cot F, cot G, cot H, cot D duoc trich tu sheet DMHH o phan duoi
With Sheets("Bao Cao")
Irow = .Range("B" & Rows.Count).End(xlUp).Row
Nguon = .Range("C5").Resize(Irow, 11).Value
End With
Irow = UBound(Nguon)
ReDim Kq(1 To Irow, 1 To 8)
For a = 1 To Irow - 1
Key = Nguon(a, 1) & "#" & Nguon(a, 2) & "#" & Nguon(a, 3) & "#" & Nguon(a, 6)
If Not Dic.exists(Key) Then
Dong = Dong + 1
Dic.Add Key, Dong
Kq(Dong, 1) = Nguon(a, 1)
Kq(Dong, 2) = Nguon(a, 2)
Kq(Dong, 3) = Nguon(a, 3)
Kq(Dong, 5) = Nguon(a, 6)
Kq(Dong, 6) = Nguon(a, 9)
Kq(Dong, 7) = Nguon(a, 10)
Kq(Dong, 8) = Nguon(a, 11)
Else
ViTri = Dic.Item(Key)
Kq(ViTri, 6) = Kq(ViTri, 6) + Nguon(a, 9)
Kq(ViTri, 7) = Kq(ViTri, 7) + Nguon(a, 10)
Kq(ViTri, 8) = Kq(ViTri, 8) + Nguon(a, 11)
End If
Next
With Sheets("TH Ton")
If Dong > 0 Then
.Range("A5
10000").ClearContents
.Range("A5").Resize(Dong, 8).Value = Kq
End If
End With
'-----------------------------------------------------------------
'phan trich loc tu sheet DMHH cho cot D
Rws = Sheets("TH Ton").Range("C" & Rows.Count).End(xlUp).Row
Kq() = Sheets("TH Ton").Range("C5
" & Rws).Value
W = Sheets("DMHH").Range("B" & Rows.Count).End(xlUp).Row
aDL() = Sheets("DMHH").Range("B4").Resize(W, 7).Value
For j = 1 To UBound(Kq())
For W = 1 To UBound(aDL())
If UCase$(Kq(j, 1)) = UCase$(aDL(W, 1)) Then
Kq(j, 2) = aDL(W, 7)
End If
Next W
Next j
Sheets("TH Ton").Range("C5").Resize(Rws - 1, 2).Value = Kq()
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Sub ABC()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim Dic As Object
Dim Nguon(), Kq(), Key, ViTri, SoLan
Dim Dong, Irow, a As Long
Dim Tmr As Double, j As Long, W As Integer, Rws As Long
Dim arr(), aDL()
Set Dic = CreateObject("Scripting.Dictionary")
'------------------------------------------------------------------------------------------------------------------------------
'phan tinh cot A, cot B, cot C, cot E, cot F, cot G, cot H, cot D duoc trich tu sheet DMHH o phan duoi
With Sheets("Bao Cao")
Irow = .Range("B" & Rows.Count).End(xlUp).Row
Nguon = .Range("C5").Resize(Irow, 11).Value
End With
Irow = UBound(Nguon)
ReDim Kq(1 To Irow, 1 To 8)
For a = 1 To Irow - 1
Key = Nguon(a, 1) & "#" & Nguon(a, 2) & "#" & Nguon(a, 3) & "#" & Nguon(a, 6)
If Not Dic.exists(Key) Then
Dong = Dong + 1
Dic.Add Key, Dong
Kq(Dong, 1) = Nguon(a, 1)
Kq(Dong, 2) = Nguon(a, 2)
Kq(Dong, 3) = Nguon(a, 3)
Kq(Dong, 5) = Nguon(a, 6)
Kq(Dong, 6) = Nguon(a, 9)
Kq(Dong, 7) = Nguon(a, 10)
Kq(Dong, 8) = Nguon(a, 11)
Else
ViTri = Dic.Item(Key)
Kq(ViTri, 6) = Kq(ViTri, 6) + Nguon(a, 9)
Kq(ViTri, 7) = Kq(ViTri, 7) + Nguon(a, 10)
Kq(ViTri, 8) = Kq(ViTri, 8) + Nguon(a, 11)
End If
Next
With Sheets("TH Ton")
If Dong > 0 Then
.Range("A5

.Range("A5").Resize(Dong, 8).Value = Kq
End If
End With
'-----------------------------------------------------------------
'phan trich loc tu sheet DMHH cho cot D
Rws = Sheets("TH Ton").Range("C" & Rows.Count).End(xlUp).Row
Kq() = Sheets("TH Ton").Range("C5

W = Sheets("DMHH").Range("B" & Rows.Count).End(xlUp).Row
aDL() = Sheets("DMHH").Range("B4").Resize(W, 7).Value
For j = 1 To UBound(Kq())
For W = 1 To UBound(aDL())
If UCase$(Kq(j, 1)) = UCase$(aDL(W, 1)) Then
Kq(j, 2) = aDL(W, 7)
End If
Next W
Next j
Sheets("TH Ton").Range("C5").Resize(Rws - 1, 2).Value = Kq()
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub