Sub CapNhatDmHH()
With Application
.Calculation = xlCalculationManual: .ScreenUpdating = False
End With
Dim endR As Long, i As Long
Dim CK2 As Double, CK3 As Double
Dim ArrMaHH(), ArrCK02(), ArrCK03()
Dim rngDM As Range, rngCK02 As Range, rngCK03 As Range
'xoa DMHH
With Sheets("DMHH").Range("A4")
endR = .Cells(1000, 2).End(xlUp).Row 'Lay theo cot tenHH'
.Resize(endR, 4).ClearContents 'Xoa DM
.Offset(, 7).Resize(endR, 1).ClearContents 'Xoa CK2'
.Offset(, 9).Resize(endR, 1).ClearContents 'Xoa CK3'
End With
With Sheets("BGia")
endR = .Cells(1000, 2).End(xlUp).Row 'Lay theo cot tenHH'
ArrMaHH = .Range("A8:D" & endR).Value
Set rngCK02 = .Range("E8:E" & endR) 'vung co gia 2'
Set rngCK03 = .Range("G8:G" & endR) 'vung co gia 3'
End With
endR = endR - 8 + 1: CK2 = 0: CK3 = 0
ReDim ArrCK02(1 To endR, 1 To 1), ArrCK03(1 To endR, 1 To 1)
For i = 1 To endR
'xet theo vung co CK2'
If rngCK02(i).MergeCells = True Then
If rngCK02(i) = 0 Then
CK2 = CK2
Else
CK2 = rngCK02(i)
End If
ArrCK02(i, 1) = CK2
Else
ArrCK02(i, 1) = rngCK02(i)
End If
'xet theo vung co CK3'
If rngCK03(i).MergeCells = True Then
If rngCK03(i) = 0 Then
CK3 = CK3
Else
CK3 = rngCK03(i)
End If
ArrCK03(i, 1) = CK3
Else
ArrCK03(i, 1) = rngCK03(i)
End If
Next i
'Gan lai vao DMHH
With Sheets("DMHH").Range("A4")
.Resize(endR, 4) = ArrMaHH 'Gan tenHH, Mhh..'
.Offset(, 7).Resize(endR, 1) = ArrCK02 'gan vao CK2'
.Offset(, 9).Resize(endR, 1) = ArrCK03 'gan vao CK3'
End With
Set rngDM = Nothing: Set rngCK02 = Nothing: Set rngCK03 = Nothing
Erase ArrMaHH(), ArrCK02(), ArrCK03()
With Application
.Calculation = xlCalculationAutomatic: .ScreenUpdating = True
End With
End Sub