Re: Correct VB code to close a workbook
So, just in case it helps anyone else out, I ended up with this.
Code
Sub tester_filter()
' Filter list of testers from Plan to dashboard and show how many line items each one is working on
' Set file path for temp worksheet (if it doesn't exits just create temp.xls and enter file path below)
Dim Temp As String
Dim NewBook As Workbook
Dim wbSource As Workbook
Dim RowCount As Long
Set wbSource = ActiveWorkbook
'No longer needed
'Temp = wbSource.Sheets("Dashboard").Range("j2").value
' Check that filename has been set
' If Temp = "" Then
' MsgBox "Please set the temp file location before you continue", vbCritical, "File Location"
' Exit Sub
' End If
' If Dir(Temp) <> "" Then
' MsgBox "Temp file found. Click OK to continue", vbInformation, "Success"
' Else
' MsgBox "Temp file doesn't exist, please check the file path & name and try again", vbCritical, "File not found"
' Exit Sub
' End If
' Turn off screen flicker
Application.ScreenUpdating = False
' open temp workbook
' Set NewBook = Workbooks.Open(Filename:=Temp)
' create a new workbook
Set NewBook = Workbooks.Add
With NewBook
End With
' Select worksheet dashboard and delete current list
With wbSource
.Sheets("Dashboard").Rows("24:100").Delete Shift:=xlUp
'Copy range of testers to new sheet
.Sheets("Plan").Range("D3:D200").Copy NewBook.ActiveSheet.Range("A1")
End With
With NewBook.ActiveSheet
.Range("a1:a200").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range( _
"b1"), Unique:=True
' Paste new filtered list to dashboard
.Range("b2:b200").Copy Destination:=wbSource.Sheets("Dashboard").Range("B24")
End With
' close temp book
NewBook.Close SaveChanges:=False
' Sort list into alphabetical order
With wbSource
With .Worksheets("Dashboard").Sort
With .SortFields
.Clear
.Add Key:=wbSource.Worksheets("Dashboard").Range("B24"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
End With
.SetRange wbSource.Worksheets("Dashboard").Range("B24:B50")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Add Auto Filter to Plan Sheet
.Sheets("Plan").Range("A2:T2").AutoFilter
' Select dashboard, update formula and draw borders
With .Sheets("Dashboard")
RowCount = Application.WorksheetFunction.CountA(.Range("b24:b100"))
With .Range("c24:c" & RowCount + 23)
.FormulaR1C1 = "=COUNTIF(Plan!R3C4:R200C4,RC[-1])"
.Borders.LineStyle = xlContinuous
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
End With
End With
' Turn on screen updating
Application.ScreenUpdating = True
End Sub
Display More