studentlearner
New Member
- Joined
- Oct 7, 2021
- Messages
- 30
- Office Version
- 365
- Platform
- Windows
So the function is working as how I intended, just that after it finishes saving as it keeps the last workbook opened and the last created worksheet still opened. how do I prevent the error from popping up?
Sub CreateBranchSheets()
Dim BranchField As Range
Dim BranchName As Range
Dim NewWSheet As Worksheet
Dim WSheet As Worksheet
Dim WSheetFound As Boolean
Dim DataWSheet As Worksheet
Set DataWSheet = Worksheets("Schedule")
Set BranchField = DataWSheet.Range("A2", DataWSheet.Range("A2").End(xlDown))
Application.ScreenUpdating = False
'Loop through each branch name in column D
For Each BranchName In BranchField
'Check whether the current branch name corresponds with an existing sheet name
For Each WSheet In ThisWorkbook.Worksheets
If WSheet.Name = BranchName Then
WSheetFound = True
Exit For ' if it does assign True to the WSheetFound variable and exit the For Each Next Loop
Else
WSheetFound = False ' if it doesn't assign False to the WSheetFound variable
End If
Next WSheet
If WSheetFound Then 'if WSheetFound = True
'copy and paste the record to the relevant worksheet, in the next available row
Else 'if WSheetFound = False
Sheets("Rubrics").Copy After:=Sheets(9)
ActiveSheet.Name = BranchName
ActiveSheet.Range("D1").Value = "Project ID: " & BranchName
ActiveSheet.Range("D2").Value = "Project Title: " & BranchName.Offset(1, 3)
Application.DisplayAlerts = False
ActiveSheet.Copy
With ActiveWorkbook
.SaveAs Filename:="C:\Users\john\Desktop\Project\" & BranchName.Offset(1, 2) & "\" & BranchName & "\" & BranchName & ".xlsx", FileFormat:=51
.Close False
End With
ActiveSheet.Delete
Application.DisplayAlerts = True
End If
Next BranchName
'autofit columns in each sheet in the workbook
For Each WSheet In ThisWorkbook.Worksheets
WSheet.UsedRange.Columns.AutoFit
Next WSheet
Application.ScreenUpdating = True
End Sub
Sub CreateBranchSheets()
Dim BranchField As Range
Dim BranchName As Range
Dim NewWSheet As Worksheet
Dim WSheet As Worksheet
Dim WSheetFound As Boolean
Dim DataWSheet As Worksheet
Set DataWSheet = Worksheets("Schedule")
Set BranchField = DataWSheet.Range("A2", DataWSheet.Range("A2").End(xlDown))
Application.ScreenUpdating = False
'Loop through each branch name in column D
For Each BranchName In BranchField
'Check whether the current branch name corresponds with an existing sheet name
For Each WSheet In ThisWorkbook.Worksheets
If WSheet.Name = BranchName Then
WSheetFound = True
Exit For ' if it does assign True to the WSheetFound variable and exit the For Each Next Loop
Else
WSheetFound = False ' if it doesn't assign False to the WSheetFound variable
End If
Next WSheet
If WSheetFound Then 'if WSheetFound = True
'copy and paste the record to the relevant worksheet, in the next available row
Else 'if WSheetFound = False
Sheets("Rubrics").Copy After:=Sheets(9)
ActiveSheet.Name = BranchName
ActiveSheet.Range("D1").Value = "Project ID: " & BranchName
ActiveSheet.Range("D2").Value = "Project Title: " & BranchName.Offset(1, 3)
Application.DisplayAlerts = False
ActiveSheet.Copy
With ActiveWorkbook
.SaveAs Filename:="C:\Users\john\Desktop\Project\" & BranchName.Offset(1, 2) & "\" & BranchName & "\" & BranchName & ".xlsx", FileFormat:=51
.Close False
End With
ActiveSheet.Delete
Application.DisplayAlerts = True
End If
Next BranchName
'autofit columns in each sheet in the workbook
For Each WSheet In ThisWorkbook.Worksheets
WSheet.UsedRange.Columns.AutoFit
Next WSheet
Application.ScreenUpdating = True
End Sub