Sub intungho()
Dim i As Integer
Dim x As Range
Dim y As Range
On Error GoTo last
Dim cotho As Range
Dim cell As Range
Dim z As Integer
Dim hoinx As Integer
Dim hodaux As Integer
Dim hocuoix As Integer
Dim honhay As Integer
Set cotho = Range(Range("A5"), Range("tongcong").Offset(-1, 0))
'Columns("I:I").EntireColumn.Hidden = True ' ?n c?t I
hoinx = Range("O2").Value 'in h? c?n in
hodaux = Range("O1").Value 'in t?
hocuoix = Range("P1").Value ' ??n h?
'Kiem tra dieu kien vao
If hoinx > 0 Then
cotho.EntireRow.Hidden = True '???c s? d?ng ?? ?n toàn b? các hàng trong vùng cotho (các hàng t? dòng 9 ??n dòng "tongcong" - 1).
For Each cell In cotho
If cell.Value = 1 And cell.Offset(0, 1).Value = hoinx Then
z = 0
Do
z = z + 1
Loop While cell.Offset(z, 0).Value = 0
Set y = Range(cell.Offset(0, 1), cell.Offset(z - 1, 9))
y.EntireRow.Hidden = False
End If
Next
ElseIf hodaux > 0 And hocuoix > 0 Then
cotho.EntireRow.Hidden = True
For honhay = hodaux To hocuoix
For Each cell In cotho
If cell.Value = 1 And cell.Offset(0, 1).Value = honhay Then
z = 0
Do
z = z + 1
Loop While cell.Offset(z, 0).Value = 0
Set y = Range(cell.Offset(0, 1), cell.Offset(z - 1, 9))
y.EntireRow.Hidden = False
End If
Next
Next honhay
Else:
cotho.EntireRow.Hidden = False
End If
'Tien hanh in
For Each cell In cotho
If cell.Value = 1 And cell.EntireRow.Hidden = False Then
z = 0
Do
z = z + 1
Loop While cell.Offset(z, 0).Value = 0
Set y = Range(cell.Offset(0, 1), cell.Offset(z - 1, 9))
Set x = Range(Cells(1, 2), cell.Offset(z - 1, 9))
ActiveSheet.PageSetup.PrintArea = x.Address
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
y.EntireRow.Hidden = True
End If
Next
'Quay lai
cotho.EntireRow.Hidden = False
'Columns("I:I").EntireColumn.Hidden = False
Range("O2").Value = ""
Range("O1").Value = ""
Range("P1").Value = ""
last: Exit Sub
End Sub