Option Explicit
Dim MyTmp02 As String, MyAdd02 As String, MyTmp03 As String, MyAdd03 As String
Dim endR As Long, r As Long
Dim ArrMaHH(), ArrCK02(), ArrCK03()
Dim rngDM As Range, rngCK02 As Range, rngCK03 As Range
Sub CapNhatDmHH()
With Application
.Calculation = xlCalculationManual: .ScreenUpdating = False
End With
'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
ReDim ArrCK02(1 To endR, 1 To 1), ArrCK03(1 To endR, 1 To 1)
MyAdd02 = "": MyAdd03 = ""
For r = 1 To endR
'Phan CK2'
If rngCK02(r, 1).MergeCells Then
MyTmp02 = rngCK02(r, 1).MergeArea.Address
If InStr(MyAdd02, MyTmp02) = False Then
MyAdd02 = MyAdd02 & MyTmp02
ArrCK02(r, 1) = rngCK02(r, 1).Value
Else
ArrCK02(r, 1) = ArrCK02(r - 1, 1)
End If
Else
ArrCK02(r, 1) = rngCK02(r, 1).Value
End If
'Phan CK3'
If rngCK03(r, 1).MergeCells Then
MyTmp03 = rngCK03(r, 1).MergeArea.Address
If InStr(MyAdd03, MyTmp03) = False Then
MyAdd03 = MyAdd03 & MyTmp03
ArrCK03(r, 1) = rngCK03(r, 1).Value
Else
ArrCK03(r, 1) = ArrCK03(r - 1, 1)
End If
Else
ArrCK03(r, 1) = rngCK03(r, 1).Value
End If
Next r
With Sheets("BGIA")
.[J8].Resize(r - 1, 1) = ArrCK02
.[K8].Resize(r - 1, 1) = ArrCK03
End With
'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