Private tVitri
'Su kien ghi nhan gia tri truoc khi thay doi cua cot J - No of branch
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveSheet
'kiem tra gia tri tai cot J nhap vao phai la <Kieu So>
If IsNumeric(.Cells(Target.Row, 9).Value) = False Then
MsgBox "Du lieu nhap vao cot I phai la <Kieu So>", vbInformation, "Thong Bao"
Exit Sub
End If
'xac dinh gia tri tai cot J truoc khi thay doi
If Target.Count = 1 And Target.Column = 9 And Target.Row > 10 Then
tVitri = Round(.Cells(Target.Row, 9).Value, 0)
End If
End With
End Sub
'Su kien ghi nhan gia tri sau khi thay doi cua cot J - No of branch
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
Dim tNum As Integer, tRange As Long
With ActiveSheet
Application.ScreenUpdating = False
If Target.Count = 1 And Target.Column = 9 And Target.Row > 10 Then
'kiem tra gia tri tai cot J nhap vao phai la <Kieu So>
If IsNumeric(.Cells(Target.Row, 9).Value) = False Then
MsgBox "Du lieu nhap vao cot I phai la <Kieu So>", vbInformation, "Thong Bao"
Exit Sub
End If
tNum = Round(.Cells(Target.Row, 9).Value, 0)
'kiem tra xoa dong trong ben duoi neu vung ben duoi khong co du lieu
If tNum = 0 Then
.Range("A" & Target.Row, "N" & Target.Row).EntireRow.Delete
ElseIf tNum > 0 Then
tRange = Application.CountA(.Range("A" & Target.Row + 1, "N" & Target.Row + tVitri))
If tRange = 0 Then
.Range("A" & Target.Row + 1, "N" & Target.Row + tVitri).EntireRow.Delete
End If
End If
'Them so dong theo gia tri nhap vao tai cot J
For i = 1 To tNum
.Cells(Target.Row + 1, Target.Column).EntireRow.Insert
Next
End If
Application.ScreenUpdating = True
End With
End Sub