Option Explicit
Sub CopyTo()
Application.ScreenUpdating = False
Dim lRow, lrow2 As Long, jJ As Long, dong As Long
Shdky.Select
lRow = [B65432].End(xlUp).Row
Application.ScreenUpdating = False
ShDh.Range("A7:J" & lRow).Clear
ShDh.Range("g:h").NumberFormat = "@"
lrow2 = ShDh.[B65432].End(xlUp).Row + 1
For jJ = 4 To lRow
With Cells(jJ, 7)
If .Value <> "" Or .Offset(, 1) <> "" Then
If .Offset(, 1) <> "" Then
dong = TimDong(.Offset(0, 4), ShDh.Range("H7:H" & lrow2 + 1))
If dong = 0 Then
ShDh.Range("A" & lrow2) = .Offset(, -6).Value
ShDh.Range("B" & lrow2) = .Offset(, -5).Value
ShDh.Range("C" & lrow2) = .Offset(, -4).Value
ShDh.Range("D" & lrow2) = .Offset(, -3).Value
ShDh.Range("E" & lrow2) = .Offset(, -2).Value
ShDh.Range("F" & lrow2) = .Offset(, -1).Value
ShDh.Range("G" & lrow2) = .Offset(, 3).Value
ShDh.Range("H" & lrow2) = .Offset(, 4).Value
lrow2 = lrow2 + 1
Else
MsgBox "GT dang co: " & ShDh.Cells(6, 6).Offset(dong, 0).Value
ShDh.Cells(6, 6).Offset(dong, 0) = ShDh.Cells(6, 6).Offset(dong, 0).Value + .Offset(, -1).Value
End If
Else
ShDh.Range("A" & lrow2) = .Offset(, -6).Value
ShDh.Range("B" & lrow2) = .Offset(, -5).Value
ShDh.Range("C" & lrow2) = .Offset(, -4).Value
ShDh.Range("D" & lrow2) = .Offset(, -3).Value
ShDh.Range("E" & lrow2) = .Offset(, -2).Value
ShDh.Range("F" & lrow2) = .Offset(, -1).Value
ShDh.Range("G" & lrow2) = .Offset(, 3).Value
ShDh.Range("H" & lrow2) = .Offset(, 4).Value
lrow2 = lrow2 + 1
End If
Else
If .Offset(, 3).Value <> "" Then .Offset(, 3).Value = ""
If .Offset(, 4).Value <> "" Then .Offset(, 4).Value = ""
End If
End With
Next jJ
ShDh.Range("A7:J" & lrow2).Font.Name = ".vnTime"
'TAO DONG TONG VA ForMAT dong
With ShDh
.Cells(lrow2 + 1, 5).Font.Name = ".VNTIMEH"
.Cells(lrow2 + 1, 5).Formula = "TæNG Sè CP Dù Häp"
.Cells(lrow2 + 1, 5).HorizontalAlignment = xlRight
.Cells(lrow2 + 1, 5).VerticalAlignment = xlCenter
.Cells(lrow2 + 1, 5).Font.Bold = True
.Cells(lrow2 + 1, 6).Formula = "=SUM(F7:F" & lrow2 & ")"
.Cells(lrow2 + 1, 6).HorizontalAlignment = xlRight
.Cells(lrow2 + 1, 6).VerticalAlignment = xlCenter
.Cells(lrow2 + 1, 6).Font.Bold = True
.Cells(lrow2 + 2, 5).Font.Name = ".VNTIMEH"
.Cells(lrow2 + 2, 5).Formula = "Tû LÖ CP Dù Häp"
.Cells(lrow2 + 2, 5).HorizontalAlignment = xlRight
.Cells(lrow2 + 2, 5).VerticalAlignment = xlCenter
.Cells(lrow2 + 2, 5).Font.Bold = True
.Cells(lrow2 + 2, 6).Formula = "=ROUND(SUM(F7:F" & lrow2 & ")*100/$F$4,3)"
.Cells(lrow2 + 2, 6).HorizontalAlignment = xlRight
.Cells(lrow2 + 2, 6).VerticalAlignment = xlCenter
.Cells(lrow2 + 2, 6).Font.Bold = True
.Cells(lrow2 + 2, 7).Formula = "%"
.Cells(lrow2 + 2, 7).HorizontalAlignment = xlLeft
.Cells(lrow2 + 2, 7).VerticalAlignment = xlCenter
.Cells(lrow2 + 2, 7).Font.Bold = True
End With
ShDh.Range("D7:D" & lrow2).HorizontalAlignment = xlRight
ShDh.Range("E7:E" & lrow2).HorizontalAlignment = xlCenter
ShDh.Range("G7:G" & lrow2).HorizontalAlignment = xlRight
ShDh.Range("H7:H" & lrow2).HorizontalAlignment = xlCenter
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Function TimDong(Ma As String, Mang As Range) As Long
On Error Resume Next
TimDong = WorksheetFunction.Match(Ma, Mang, 0)
End Function