Option Explicit
Option Private Module 'De Pubilc chi co tac dung trong all module in this project only
Public WbColecRgn As Workbook, WbCopyRng As Workbook, WbCriteriaRng As Workbook
'MyWks As Worksheet
Public MyRngColection As Range, MyFilterAction As String
Public iRow As Integer
Public tRgColecWbDir As String, tRgColecWb As String, tRgColecWks As String, tRgColec As String
Public MyCriteriaDir As String, MyCriteriaWb As String, MyCriteriaWk As String, MyCriteriaRng As String
Public MyCopyToRangeDir As String, MyCopyToRangeWb As String, MyCopyToRangeWk As String, MyCopyToRangeRng As String
Public Const MyFilterActionCopy As Integer = 2
Public Const MyFilterActionInPlace As Integer = 1
Public MyCriteriaRange As Variant, MyCopyToRange As Variant, MyUnique As Variant
Sub FilterInKTTT_Main1()
iRow = 3
Call Main(iRow)
End Sub
Sub Main(iRow As Integer)
Application.ScreenUpdating = False
Call SetColectionRangeSource
Call SetCriteriaAndCopyToRange
'Can than khi dung InPlace va xoa Name, se co cac dong an hoi kho nhin de nham la kg co, phai unhide BANG Cells.Height
If MyFilterAction = "xlFilterCopy" Then
Call MyAdvancedFilter(MyFilterActionCopy, MyCriteriaRange, MyCopyToRange, MyUnique)
Else
Call MyAdvancedFilter1(MyFilterActionInPlace, MyCriteriaRange, MyCopyToRange, MyUnique)
End If
On Error Resume Next
WbCopyRng.Names("Extract").Delete
WbColecRgn.Names("_FilterDatabase").Delete
'Call DeleteFilterName
WbColecRgn.Close True
WbCriteriaRng.Close True
Set WbColecRgn = Nothing
Set WbCriteriaRng = Nothing
Set WbCopyRng = Nothing
Application.ScreenUpdating = True
End Sub
Sub SetColectionRangeSource()
With ThisWorkbook.Worksheets("Sheet1")
tRgColecWbDir = .Cells(iRow, 2)
tRgColecWb = .Cells(iRow, 3)
tRgColecWks = .Cells(iRow, 4)
tRgColec = .Cells(iRow, 5)
If bIsBookOpen(.Cells(iRow, 3)) Then
Set WbColecRgn = Workbooks(tRgColecWb)
'Set MyWks = Worsheets(tRgColecWks)
Else
Set WbColecRgn = Workbooks.Open(tRgColecWbDir & tRgColecWb)
'Set MyWks = Worsheets(tRgColecWks)
End If
Set MyRngColection = WbColecRgn.Worksheets(tRgColecWks).Range(tRgColec)
End With
'MyRngColection.AdvancedFilter MyFilterActionCopy, MyCriteriaRange, MyCopyToRange, MyUnique ok
'Call DeleteFilterName
'
End Sub
Sub SetCriteriaAndCopyToRange()
With ThisWorkbook.Worksheets("Sheet1")
MyCriteriaDir = .Cells(iRow, 6)
MyCriteriaWb = .Cells(iRow, 7)
MyCriteriaWk = .Cells(iRow, 8)
MyCriteriaRng = .Cells(iRow, 9)
If bIsBookOpen(MyCriteriaWb) Then
Set WbCriteriaRng = Workbooks(MyCriteriaWb)
Else
Set WbCriteriaRng = Workbooks.Open(MyCriteriaDir & MyCriteriaWb)
End If
MyCopyToRangeDir = .Cells(iRow, 10)
MyCopyToRangeWb = .Cells(iRow, 11)
MyCopyToRangeWk = .Cells(iRow, 12)
MyCopyToRangeRng = .Cells(iRow, 13)
If bIsBookOpen(MyCopyToRangeWb) Then
Set WbCopyRng = Workbooks(MyCopyToRangeWb)
Else
Set WbCopyRng = Workbooks.Open(MyCopyToRangeDir & MyCopyToRangeWb)
End If
Set MyCriteriaRange = Workbooks(MyCriteriaWb).Worksheets(MyCriteriaWk).Range(MyCriteriaRng)
Set MyCopyToRange = Workbooks(MyCopyToRangeWb).Worksheets(MyCopyToRangeWk).Range(MyCopyToRangeRng)
'MyCopyToRange.Select
MyUnique = .Cells(iRow, 15)
MyFilterAction = .Cells(2, 14)
End With
Application.ScreenUpdating = True
End Sub
Sub MyAdvancedFilter(MyFilterActionCpy As Integer, MyCriteriaRange As Variant, MyCopyToRange As Variant, MyUnique As Variant)
'Khi vung Extract va Database cung 1 wb va sua Public bi loi? KHG
'xOA bot name trong file KTTT_Main
MyRngColection.AdvancedFilter MyFilterActionCopy, MyCriteriaRange, MyCopyToRange, MyUnique
End Sub
Sub MyAdvancedFilter1(MyFilterActionInPlace As Integer, MyCriteriaRange As Variant, MyCopyToRange As Variant, MyUnique As Variant)
MyRngColection.AdvancedFilter1 MyFilterActionInPlace, MyCriteriaRange, MyCopyToRange, MyUnique
End Sub
Sub DeleteFilterName()
On Error Resume Next
With ActiveWorkbook
.Names("_FilterDatabase").Delete
.Names("Criteria").Delete
.Names("Extract").Delete
End With
End Sub
Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function