Xin hỏi có cach nào lọc trong VBA, lọc không trùng nhau như file mấu.
Chi lọc 1 giá trị 12 duy nhất trong nhiều giá trị 12
" Trong cột : MA1 chỉ lọc giá trị không trùng nhau :
KQ
12 x x x
15 x x x
Sub Loc()
Dim Rng As Range, Clls As Range, TempRng As Range, DkRng As Range
Dim i As Long
Set Rng = [A5].CurrentRegion
[H5].CurrentRegion.Clear
Rng.Resize(, 1).AdvancedFilter 2, , [H5], True
With [H5].CurrentRegion
Set DkRng = .Offset(1).Resize(.Rows.Count - 1)
End With
For Each Clls In DkRng
Set TempRng = Rng.Resize(, 1).Find(Clls).Resize(, Rng.Columns.Count)
With TempRng
[H5].Offset(i + 1).Resize(, .Columns.Count) = .Value
i = i + 1
End With
Next
[H5].Resize(, Rng.Columns.Count).Value = Rng.Resize(1).Value
End Sub
Xin hỏi có cach nào lọc trong VBA, lọc không trùng nhau như file mấu.
Chi lọc 1 giá trị 12 duy nhất trong nhiều giá trị 12
" Trong cột : MA1 chỉ lọc giá trị không trùng nhau :
KQ
12 x x x
15 x x x
Function DanhSach(MangDL As Range, Optional MangMa As Range)
On Error Resume Next
Dim i As Long
If MangDL.Rows.Count < 1 Then Exit Function
If MangMa.Rows.Count = 0 Then DanhSach = MangDL(1): Exit Function
For i = 1 To MangDL.Rows.Count
If WorksheetFunction.CountIf(MangMa, MangDL(i)) = 0 Then
DanhSach = MangDL(i)
Exit For
End If: Next
End Function
Function DanhSachM(MangDL As Range)
Application.ScreenUpdating = False
Dim i As Long, i2, i1 As Long, m As Integer, Tim As Boolean, Ma As Range
Dim MangTemp(1 To 1000, 0) As Variant
Dim Mang(1 To 1000, 0)
If MangDL.Rows.Count = 0 Then Exit Function
For Each Ma In MangDL
i = i + 1
If i = 1 Then
m = m + 1
MangTemp(m, 0) = Ma.Value
Else
For i1 = 1 To m
If UCase(MangTemp(i1, 0)) = UCase(Ma) Then
Tim = True
Exit For
End If
Next i1
If Tim = False Then
m = m + 1
MangTemp(m, 0) = Ma.Value
End If
End If
Tim = False
Next
DanhSachM = MangTemp()
Set Ma = Nothing
Application.ScreenUpdating = True
End Function
Function DanhSachMSX(MangDL As Range)
Application.ScreenUpdating = False
Dim i As Long, i2, i1 As Long, m As Integer, Tim As Boolean, Ma As Range
Dim MangTemp(1 To 1000, 0) As Variant
Dim Mang(1 To 1000, 0)
If MangDL.Rows.Count = 0 Then Exit Function
For Each Ma In MangDL
i = i + 1
If i = 1 Then
m = m + 1
MangTemp(m, 0) = Ma.Value
Else
For i1 = 1 To m
If UCase(MangTemp(i1, 0)) = UCase(Ma) Then
Tim = True
Exit For
End If
Next i1
If Tim = False Then
m = m + 1
MangTemp(m, 0) = Ma.Value
End If
End If
Tim = False
Next
'Loc Danh Sach
For i = 1 To m
If i = 1 Then ' Gan PT dau tien
Mang(1, 0) = MangTemp(1, 0)
Else
For i1 = 1 To i - 1 ' Xem co nho hon GT nao trong Mang khong ??
If LCase(MangTemp(i, 0)) < LCase(Mang(i1, 0)) Then Tim = True: Exit For
Next i1
If Tim = False Then ' Khong co : Cho xuong duoi Danh Sach
Mang(i, 0) = MangTemp(i, 0)
Else ' Neu co :
For i2 = i To i1 + 1 Step -1
Mang(i2, 0) = Mang(i2 - 1, 0) 'Dich chuyen danh sach xuong 1 nac
Next i2
Mang(i1, 0) = MangTemp(i, 0) ' Cho phan tu vao DS
End If
End If
Tim = False
Next
DanhSachMSX = Mang()
Set Ma = Nothing
Application.ScreenUpdating = True
End Function