Sub project()
Dim filter As String
Dim caption As String
Dim RB_Filename As String
Dim RB_workbook As Workbook
Dim RB_sheet As Worksheet
Dim RB_Lrow As Long
Dim RB_Lcol As Long
Dim RB_rngFilter As Range
Dim Master_workbook As Workbook
Dim Master_sheet As Worksheet
Dim Aging_workbook As Workbook
Dim Aging_worksheet As Worksheet
Dim Aging_Filename As String
Dim Ws1_Lrow As Long
Dim Ws1_Lcol As Long
Dim rngCopy_Red As Range
Dim rngCopy_Black As Range
' make weak assumption that active workbook is the target
'Set Master_workbook = Application.ActiveWorkbook '.Open("Master Template.xlsm")
Set Master_workbook = ThisWorkbook
' get the R&B workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
MsgBox "Please insert R&B inventories file "
RB_Filename = Application.GetOpenFilename(filter, , caption)
' get the aging workbook
MsgBox "Please insert Aging file "
Aging_Filename = Application.GetOpenFilename(filter, , caption)
'If Cancel then exit
If TypeName(RB_Filename) = "Boolean" Then Exit Sub
Set RB_workbook = Workbooks.Open(RB_Filename, ReadOnly:=True)
Set Aging_workbook = Workbooks.Open(Aging_Filename, ReadOnly:=True)
' copy data from R&B workbook and Aging to Master_workbook
Set RB_sheet = RB_workbook.Worksheets(" qry Active")
Set Aging_sheet = Aging_workbook.Worksheets("base_data")
RB_sheet.Activate
RB_sheet.Select
Aging_sheet.Activate
Aging_sheet.Select
With Master_workbook
'first remove the 'qry Active' worksheet from Master (if it exists)
On Error Resume Next
Application.DisplayAlerts = False
.Worksheets("qry Active").Delete
.Worksheets("base_data").Delete
Application.DisplayAlerts = True
On Error GoTo 0
'copy the qry Active ws to Master
RB_sheet.Copy After:=.Sheets(.Sheets.Count)
Aging_sheet.Copy After:=.Sheets(.Sheets.Count)
End With
RB_workbook.Close savechanges:=False
Aging_workbook.Close savechanges:=False
' Filter data for red and black stock
Master_workbook.Worksheets("qry Active").Activate
'Ws1_Lrow = Cells(Rows.Count, 1).End(xlUp).Row
'Ws1_Lcol = Cells(1, Columns.Count).End(xlToLeft).Column
With Master_workbook.Worksheets("qry Active")
Ws1_Lrow = .Cells.Find("*", .Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row
Ws1_Lcol = .Cells.Find("*", .Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column
Set rngFilter = .Range(.Cells(1, 1), .Cells(Ws1_Lrow, Ws1_Lcol))
Dim Lastrow_Base As Integer
Sheets("qry Active").Columns("B:B").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=.Range("Y1"), Unique:=True
Lastrow_Base = .Cells(.Rows.Count, "AY").End(xlUp).Row 'Unique:=True
Dim i As Integer
Dim x As Range
For i = 2 To Lastrow_Base
Set x = Master_workbook.Sheets("qry Active").Range("Y" & i)
If x <> "" Then
' create New workbook and add sheets
Set NewBook = Workbooks.Add
With NewBook
.Title = x
'add your additional code here
NewBook.Activate
NewBook.Worksheets.Select
NewBook.Worksheets("sheet1").Name = "Red "
NewBook.Worksheets("sheet2").Activate
NewBook.Worksheets("sheet2").Name = "Black"
' Filter data for red stock
With rngFilter
.AutoFilter Field:=5, Criteria1:="Red ", Operator:=xlFilterValues
.AutoFilter Field:=4, Criteria1:="Standard", Operator:=xlFilterValues
.AutoFilter Field:=25, Criteria1:=x.Value, Operator:=xlFilterValues
Set rngCopy_Red = .SpecialCells(xlCellTypeVisible)
.AutoFilter ' Switch off AutoFilter
End With
rngCopy_Red.Copy NewBook.Worksheets("Red").Cells(1, 1)
' Filter data for Black stock
With rngFilter
.AutoFilter Field:=5, Criteria1:="Black ", Operator:=xlFilterValues
.AutoFilter Field:=4, Criteria1:="Standard", Operator:=xlFilterValues
.AutoFilter Field:=25, Criteria1:=x.Value, Operator:=xlFilterValues
Set rngCopy_Red = .SpecialCells(xlCellTypeVisible)
.AutoFilter ' Switch off AutoFilter
End With
rngCopy_Red.Copy NewBook.Worksheets("Black").Cells(1, 1)
.SaveAs Filename:="KPI" & " " & x & ".xlsx"
NewBook.Close
End With
Else
If x = "" Then
Set NewBook = Workbooks.Add
With NewBook
.Title = x
' add your additional code here
NewBook.Activate
NewBook.Worksheets.Select
NewBook.Worksheets("sheet1").Name = "Red "
NewBook.Worksheets("sheet2").Activate
NewBook.Worksheets("sheet2").Name = "Black "
With rngFilter
.AutoFilter Field:=5, Criteria1:="Red ", Operator:=xlFilterValues
.AutoFilter Field:=4, Criteria1:="Standard", Operator:=xlFilterValues
.AutoFilter Field:=25, Criteria1:=x.Value, Operator:=xlFilterValues
Set rngCopy_Red = .SpecialCells(xlCellTypeVisible)
.AutoFilter ' Switch off AutoFilter
End With
rngCopy_Red.Copy Destination:=NewBook.Worksheets("Red ").Cells(1, 1)
With rngFilter
.AutoFilter Field:=5, Criteria1:="Black ", Operator:=xlFilterValues
.AutoFilter Field:=4, Criteria1:="Standard Component", Operator:=xlFilterValues
.AutoFilter Field:=25, Criteria1:=x.Value, Operator:=xlFilterValues
Set rngCopy_Black = .SpecialCells(xlCellTypeVisible)
.AutoFilter ' Switch off AutoFilter
End With
rngCopy_Black.Copy Destination:=NewBook.Worksheets("Black").Cells(1, 1)
.SaveAs Filename:="KPI" & " " & "No SCM" & ".xlsx"
NewBook.Close
End With
End If
End If
Next
End With
End Sub
Note: I have written code only for one source book and there are many other source workbooks which needs to be sorted out.
Bookmarks