-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBA - XÓA DÒNG THEO ĐIỀU KIỆN
62 lines (47 loc) · 1.73 KB
/
VBA - XÓA DÒNG THEO ĐIỀU KIỆN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
VBA - XÓA DÒNG THEO ĐIỀU KIỆN
Function CollecToArray(c As Collection) As Variant()
Dim a() As Variant: ReDim a(0 To c.Count - 1)
Dim i As Integer
For i = 1 To c.Count
a(i - 1) = c.Item(i)
Next
CollecToArray = a
End Function
Sub Xoa_Row_Theo_Dieu_Kien()
Dim Collec As Collection
Set Collec = New Collection
If MsgBox("ARE YOU SURE ?" & vbCrLf & vbCrLf & " THIS ACTION CAN'T UNDO", vbYesNo, "Thông báo") <> vbYes Then
Exit Sub
End If
If Selection.Rows.Count <= 0 Then Exit Sub
Dim Mode As String
Mode = InputBox("Please Choose Mode" & vbCrLf & vbCrLf & "Mode 1 = DELETE ROW IF CELL NOT EQUALS" & vbCrLf & vbCrLf & "Mode 2 = DELETE ROW IF CELL EQUALS" & vbCrLf, "Thông báo", "")
If Trim(Mode) <> "1" And Trim(Mode) <> "2" Then Exit Sub
Dim Value_DEL As String
Value_DEL = InputBox("PLEASE ENTER VALUE", "Thông báo", "")
If Trim(Value_DEL) = "" Then Exit Sub
Dim zRange As Range, zCell As Range
Dim zCell_Begin As String: zCell_Begin = ""
Set zRange = Selection
For Each zCell In zRange
If zCell_Begin = "" Then zCell_Begin = zCell.Address
If Trim(zCell.Value) <> "" Then
If Mode = "1" And UCase(Trim(zCell.Value)) <> UCase(Trim(Value_DEL)) Then
Collec.Add zCell.Row
End If
If Mode = "2" And UCase(Trim(zCell.Value)) = UCase(Trim(Value_DEL)) Then
Collec.Add zCell.Row
End If
End If
Next zCell
''' REMOVE ROW IN COLLECTION '''
If Collec.Count <> 0 Then
Dim Row_Pending_Remove() As Variant
Row_Pending_Remove = CollecToArray(Collec)
Dim i As Integer
For i = UBound(Row_Pending_Remove) To LBound(Row_Pending_Remove) Step -1
Rows(Row_Pending_Remove(i)).EntireRow.Delete
Next i
End If
ActiveSheet.Range(zCell_Begin).Select
End Sub