Steve DeWeese
New Member
I have a script (thanks to the help of others) that breaks up a master workbook into separate files for individual locations. The script copies data from 3 different tabs in a workbook to 3 different tabs in a new workbook. The script ends on the 3rd tab before saving and that is the tab that the new file opens to when opened for the first time. I would like to have it switch to the first tab before saving so that is the tab the file opens to by default. The sheet I would like it to open to is named Delivery Report. I tried adding two lines:
Sheets("Delivery Report").Select
Range("A1").Select
this did not resolve the issue.
Below is the full VBA Script. I disabled the email portion because I could not get it to work.
Sheets("Delivery Report").Select
Range("A1").Select
this did not resolve the issue.
Below is the full VBA Script. I disabled the email portion because I could not get it to work.
Code:
Sub breakMyList()
' This macro takes values in the range myList
' and breaks it in to multiple lists
' and saves them to separate files.
Dim cell As Range
Dim curPath As String
Dim wbTar As Workbook
Dim wbScr As Workbook
Dim dSht, tSht As Worksheet
Dim sRows, sCol, rr, cc As Long
Dim dStart, tStart, scrData, critRange As Range
Dim sd, st, cd, cs, rd, rt As String
curPath = ActiveWorkbook.Path & "\"
Set wbScr = ThisWorkbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each cell In Range("lstClinic")
wbScr.Activate
On Error Resume Next
ActiveSheet.ShowAllData
Err.Clear
Workbooks.Add
Set wbTar = ActiveWorkbook
wbTar.Worksheets.Add.Name = "Surgery Days"
wbTar.Worksheets.Add.Name = "Surgery Details"
wbTar.Worksheets.Add.Name = "Delivery Report"
wbScr.Activate
[valClinic] = cell.Value
Set dSht = Sheets("Delivery Report")
Set cStart = dSht.Range("Criteria_DelRep")
Set dStart = dSht.Range("myList")
dStart.Select
sRows = Selection.CurrentRegion.Rows.Count
sCol = Selection.CurrentRegion.Columns.Count
rr = dStart.Row
cc = dStart.Column
Set srcData = Range(Cells(rr, cc), Cells(rr + sRows, cc + sCol))
cStart.Select
sRows = Selection.CurrentRegion.Rows.Count
sCol = Selection.CurrentRegion.Columns.Count
rr = cStart.Row
cc = cStart.Column
Set critRange = Sheets("Delivery Report").Range(Cells(rr, cc), Cells(rr + sRows - 1, cc + sCol))
srcData.AdvancedFilter Action:=xlFilterInPlace, criteriarange:= _
critRange, Unique:=False
dStart.CurrentRegion.Select
Selection.Copy
wbTar.Sheets("Delivery Report").Paste
'Surgery Details
Set dSht = Sheets("Surgery Details")
Set dStart = dSht.Range("A1")
dSht.Activate
On Error Resume Next
ActiveSheet.ShowAllData
Err.Clear
dStart.Select
sRows = Selection.CurrentRegion.Rows.Count
sCol = Selection.CurrentRegion.Columns.Count
rr = dStart.Row
cc = dStart.Column
Set srcData = Range(Cells(rr, cc), Cells(rr + sRows, cc + sCol))
srcData.AdvancedFilter Action:=xlFilterInPlace, criteriarange:= _
critRange, Unique:=False
dStart.CurrentRegion.Select
Selection.Copy
wbTar.Sheets("Surgery Details").Paste
'Surgery Days
Set dSht = Sheets("Surgery Days")
Set dStart = dSht.Range("A1")
Set dDel = Sheets("Delivery Report")
dSht.Activate
On Error Resume Next
ActiveSheet.ShowAllData
Err.Clear
dStart.Select
sRows = Selection.CurrentRegion.Rows.Count
sCol = Selection.CurrentRegion.Columns.Count
rr = dStart.Row
cc = dStart.Column
Set srcData = Range(Cells(rr, cc), Cells(rr + sRows, cc + sCol))
srcData.AdvancedFilter Action:=xlFilterInPlace, criteriarange:= _
critRange, Unique:=False
dStart.CurrentRegion.Select
Selection.Copy
wbTar.Sheets("Surgery Days").Paste
'dDel.Activate
Sheets("Delivery Report").Select
Range("A1").Select
wbTar.SaveAs Filename:=curPath & cell.Value & Format(Now, "dmmmyyyy-hhmmss") & ".xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
'Sub Mail_workbook_Outlook_1()
'Working in Excel 2000-2013
'This example send the last saved version of the Activeworkbook
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Dim OutApp As Object
'Dim OutMail As Object
'Set OutApp = CreateObject("Outlook.Application")
'Set OutMail = OutApp.CreateItem(0)
'On Error Resume Next
'With OutMail
'.to = "sdeweese@clearchoice.com"
'.cc = ""
'.BCC = ""
'.Subject = "Delivery Report"
'.Body = "Please validate the attached delivery report"
'.Attachments.Add wbTar.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
'.Send 'or use .Display
'End With
'On Error GoTo 0
'Set OutMail = Nothing
'Set OutApp = Nothing
wbTar.Close
wbScr.Activate
Next cell
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Last edited by a moderator: