Option Explicit
''' Simple test for file existance using the File System Object. True if file exists
Function FSO_ExistFile(ByVal FilePath As String) As Boolean
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO_ExistFile = FSO.FileExists(Trim(FilePath))
End Function
Sub runCTS()
'
Dim fname As String
Dim varCellvalue As Long
Dim LastRow As Long
Dim LR As Long
Dim Cell As Range
'
'open the BI report
Application.ScreenUpdating = False
varCellvalue = Range("D1").Value
fname = "\\mdzausutwfnp001\Shardata\Logistics\Reports\Compliance to Schedule\BI Report Dump\" & varCellvalue & ".xlsm"
If Not FSO_ExistFile(fname) Then
MsgBox "File does not exist.", vbExclamation, "File Missing"
Exit Sub
Else
Workbooks.Open fname
End If
'select range of report to copy
Application.ScreenUpdating = False
Sheets("Table").Select
With Sheets("Table")
.Range("K16:Q1000" & .Cells(Rows.Count, 1).End(xlUp).Row).Select
End With
Selection.Copy
'select CTS workbook and paste
Windows("CTS Report Template.xlsm").Activate
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("BI Report Paste").Select
Application.DisplayAlerts = False
Workbooks(Range("D1").Value & ".xlsm").Close True
'Select range to convert to numbers
Sheets("BI Report Paste").Select
With Sheets("BI Report Paste")
.Range("A3:A" & .Cells(Rows.Count, 1).End(xlUp).Row).Select
End With
'delete blank lines
Application.ScreenUpdating = False
For LR = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
If Range("A" & LR).Value = "" Or Left(Range("A" & LR), 1) = "`" Then
Rows(LR).EntireRow.Delete
End If
Next LR
'convert selected range to number
On Error Resume Next
Application.ScreenUpdating = False
Selection.NumberFormat = "General"
For Each Cell In Selection
Cell.Value = Cell.Value * 1
Next Cell
Application.ScreenUpdating = True
'fill formulas down
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("H3:M" & LastRow).FillDown
'show ABS Compliance and filter largest to smallest
ActiveSheet.Range("$A$2:$M$1000").AutoFilter Field:=11, Criteria1:="<=.9", _
Operator:=xlAnd
ActiveSheet.Range("$A$2:$M$1000").AutoFilter Field:=1, Criteria1:="<>"
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("BI Report Paste").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("BI Report Paste").AutoFilter.Sort.SortFields.Add _
Key:=Range("K2:K"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("BI Report Paste").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
Application.ScreenUpdating = False
'clear old data from report page
Sheets("Report").Select
Range("B32:C50").ClearContents
Range("E32:L50").ClearContents
'select the SKU cells from the filtered CTS report
Sheets("BI Report Paste").Select
With Sheets("BI Report Paste")
.Range("A3:A" & .Cells(Rows.Count, 1).End(xlUp).Row).Copy
End With
'paste the copied cells to the report sheet
Sheets("Report").Select
Range("B32").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'select the CTS Result cells from the filtered CTS report
Sheets("BI Report Paste").Select
With Sheets("BI Report Paste")
.Range("K3:K" & .Cells(Rows.Count, 1).End(xlUp).Row).Copy
End With
'paste the copied cells to the report sheet
Sheets("Report").Select
Range("c32").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'select the Description cells from the filtered CTS report
Sheets("BI Report Paste").Select
With Sheets("BI Report Paste")
.Range("B3:B" & .Cells(Rows.Count, 1).End(xlUp).Row).Copy
End With
'paste the copied cells to the report sheet
Sheets("Report").Select
Range("E32").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("G32").Select
MsgBox ("CTS results will be copied to report page for you")
End Sub