If new year then the code will run but all sheets disappear?

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
424
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
When I save this workbook under a different name and folder all the worksheets disappear how can I avoid this??

VBA Code:
Private Sub BO_Report_Yrs()

    Dim dte As Date
    Dim numericalDate As Integer
    Dim sourceDir As String
    Dim year As Integer
    Dim Wb As Workbook
    Dim Folder_exists As String
    Dim FullFileName As String
    Dim File_Name As Variant
    Dim FolderPath As String
    Dim FilePath As String
    Dim ws As Worksheet
    Dim Rng As Range


     Set ws = ActiveSheet

         If ws.Name <> "Summary" And ws.Name <> "Trend" And ws.Name <> "Supplier BO" And ws.Name <> "Dif Depot" _
         And ws.Name <> "BO Trend WO" And ws.Name <> "BO Trend WO 2" And ws.Name <> "Different Depot" Then

            Set Wb = Workbooks.Add
            
            year = Trim(Str(Format(Date, "yyyy"))) + 1
                
            dte = Now()
            
            numerical_date = Int(CDbl(dte))
            
            sourceDir = "S:\PURCHASING\Stock Control\Alton"
            
            Folder_exists = Dir(sourceDir & "\" & year, vbDirectory)
            If Folder_exists = "" Then
                MkDir sourceDir & "\" & year
                Folder_exists = Dir(sourceDir & "\" & year, vbDirectory)
            End If
            
            FilePath = "Alton Back Order"
                FullFileName = Format(Now(), "yyyy") + 1 & " " & FilePath
                FolderPath = sourceDir & "\" & year & "\" & FullFileName
            
            Wb.SaveAs FolderPath
            
            For Each ws In Wb.Worksheets
                Set Rng = ws.Range("A2").CurrentRegion.Select
                Selection.Clear
                Next ws
            
    End If


End Sub
 
VBA Code:
Workbooks(Wb).Close SaveChanges:=False
Now says runtime error 13
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Thanks for your time your code works.
I`ve managed so sort this code would you like to see the full code?
 
Upvote 0
Yes , do post the code , so that anybody trying to do the same thing or with similar problems can see the final solution . Glad I could help
 
Upvote 0
Here you are
VBA Code:
Public Sub BO_Report_Yrs()

 
    Dim sourceDir As String
    Dim Yr As String
    Dim Month As String
    Dim Wb As Workbook
    Dim NwWb As Workbook
    Dim sht As Worksheet
    Dim Folder_exists As String
    Dim FullFileName As String
    Dim File_Name As Variant, Arr As Variant
    Dim FolderPath As String
    Dim FilePath As String
    Dim NwFilePath As String
    Dim LCol As Long
    
    
            With Application
            .ScreenUpdating = False
            .Calculation = xlManual
            .DisplayAlerts = False
            .EnableEvents = False
            End With
                    
            Yr = Trim(Str(Format(Date, "yyyy"))) + 1
            
            sourceDir = "S:\PURCHASING\Stock Control\Grays"
            
            Month = Format(Date, "mmmm")
            
            Folder_exists = Dir(sourceDir & "\" & Yr, vbDirectory)
            If Month = "December" Then
            If Folder_exists = Yr Then GoTo Application
            If Folder_exists = "" Then
                MkDir sourceDir & "\" & Yr
                Folder_exists = Dir(sourceDir & "\" & Yr, vbDirectory)
            End If
            
                FilePath = "Grays Back Order"
                FullFileName = Format(Now(), "yyyy") + 1 & " " & FilePath & ".xlsm"
                FolderPath = sourceDir & "\" & Yr & "\" & FullFileName
                                 
                Set Wb = ThisWorkbook
                Wb.SaveAs FolderPath, xlOpenXMLWorkbookMacroEnabled
                
                Set NwWb = Workbooks.Open(FolderPath)
                
                        For Each sht In NwWb.Worksheets
                        With sht
                        If .Name <> "Summary" Then
                         LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
                           Arr = .Range(.Cells(1, 1), .Cells(1, LCol))
                           .Cells.ClearContents
                           .Range(.Cells(1, 1), .Cells(1, LCol)) = Arr
                        End If
                End With
            Next sht
            
             MsgBox "New Years BO Workbook saved", vbInformation
             
Application:
            With Application
            .ScreenUpdating = True
            .Calculation = xlCalculationAutomatic
            .DisplayAlerts = True
            .EnableEvents = True
            End With
    End If
             

                                  
                    End Sub
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top