Public Sub GPE_luu()
Dim ArrData(), ArrNgay(), dArr()
Dim i As Long, j As Long, K As Long
Application.ScreenUpdating = False
'Activesheet
ArrData = Range([B1048576].End(xlUp), [L9]).Value2
ArrNgay = [C8:L8].Value2
If Range("B5") = "" Or Range("B6") = "" Or Range("B7") = "" Then
MsgBox ("So hieu, ten NV hoac lan con thieu"), vbExclamation: Exit Sub
End If
If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData 'nha loc (sheet nay` phai? mo? khoa)
'UBound(ArrData, 1) :vòng lap duyet tu dong 1 den dong cuoi' cua vung chon
'UBound(ArrNgay, 2) :vòng lap duyet tu cot 1 den cot cuoi' cua vung chon
ReDim dArr(1 To UBound(ArrData, 1) * UBound(ArrNgay, 2), 1 To 15) 'xac dinh kich thuoc mang
'duyet tung` row + cot truoc khi chuyen qua row ke' tiep
For i = 1 To UBound(ArrData, 1)
For j = 1 To UBound(ArrNgay, 2)
If ArrData(i, j + 1) <> "" Then
If tenSP <> ArrData(i, 1) Then K = K + 1 'else gan' tren cung` 1 row
dArr(K, 1) = Range("B5") 'so hieu NV
dArr(K, 2) = Range("B6") 'ten NV
dArr(K, 3) = Range("B7") 'lan
dArr(K, 4) = ArrData(i, 1) 'san pham
dArr(K, j + 4) = ArrData(i, j + 1) 'doanh so'
dArr(K, 15) = Now() 'time
tenSP = ArrData(i, 1) 'luu ten SP da~ gan' vao mang
x = K 'so san pham (ho tro border)
End If
Next j
Next i
If K Then
With Sheets("Data")
.Unprotect Password:="GPE": If .FilterMode Then .ShowAllData 'luon luon nha? loc truoc khi Update
.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(K, 15) = dArr
.Range("A" & Rows.Count).End(xlUp).Offset(-x + 1).Resize(x, 15).Borders.LineStyle = xlContinuous
.Cells.Locked = True
.Protect Password:="GPE", AllowFiltering:=True 'cho phep' loc. khi sheet Protect
End With
Erase ArrData: Erase ArrNgay: MsgBox ("Update xong"), vbInformation
Else
MsgBox ("ko co so lieu Update"), vbExclamation
End If
Application.ScreenUpdating = True
End Sub