Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90065

Code saves my file lots of times instead of one

$
0
0

Can anyone help me with my code. I am just a beginner in VBA and I manage to get few codes from internet to create an automatic saving for my file on specific time with creating a new file with a date and time. The problem is when it start creating the new file, instead of one, it create random files every second. For yesterday I had 200 files created. I have few saving code in my VBA : one after every action taken in the Workbook, one for preventing of closing the file and one to do the copy with the date and time name.

I know I have plenty of savings inside of the code but do not know which one to remove so my code to stop saving the file n-times every day.

Thank you in advance. If you need more explanations, please let me know.

The code in my workbook :


    Private Sub Workbook_AfterSave(ByVal Success As Boolean)
     'If Weekday(Date) = 5 Then
     Application.OnTime TimeValue("23:30:00"), "copySheets"
    End Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    'Step 1: Check to see if cell C7 is blank
    If sheets("Trailers").Range("Z1").Value = "" Then
    'Step 2: If cell is blank, cancel the close and tell user
        Cancel = True
        MsgBox "NOPE !!!"'Step 3: If cell is not blank, save and close
    Else
        ActiveWorkbook.Close SaveChanges:=True
    End If
    End Sub


    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Application.DisplayAlerts = False
    ActiveWorkbook.Save
    Application.DisplayAlerts = True
    End Sub

    Private Sub Workbook_WindowResize(ByVal Wn As Window)
    Wn.WindowState = xlMaximized
    ActiveWindow.EnableResize = False
    End Sub

     and The code in my Module for creating a new file with date and time. (sorry can't make this out of the code in here).

    Sub copySheets() 

    Dim wkb As Excel.Workbook
    Dim newWkb As Excel.Workbook
    Dim wks As Excel.Worksheet
    Dim newWks As Excel.Worksheet
    Dim sheets As Variant
    Dim varName As Variant
    '------------------------------------------------------------
     'Clearing all the values every Saturday
     'If Weekday(Date) = 7 Then
     'Worksheets("Trailers").Range("A3:D307").ClearContents
     'Worksheets("Trailers").Range("G3:G307").ClearContents
     ' Worksheets("Trailers").Range("J3:J307").ClearContents
      ' Worksheets("Trailers").Range("M3:M307").ClearContents
       ' Worksheets("Trailers").Range("P3:P307").ClearContents
       ' End If
       ' Application.OnTime TimeValue("23:30:00"), "copySheets"'Define the names of worksheets to be copied.
    sheets = VBA.Array("Trailers")


    'Create reference to the current Excel workbook and to the destination workbook.
    Set wkb = Excel.ThisWorkbook
    Set newWkb = Excel.Workbooks.Add


    For Each varName In sheets

        'Clear reference to the [wks] variable.
        Set wks = Nothing

        'Check if there is a worksheet with such name.
        On Error Resume Next
        Set wks = wkb.Worksheets(VBA.CStr(varName))
        On Error GoTo 0


        'If worksheet with such name is not found, those instructions are skipped.
        If Not wks Is Nothing Then

            'Copy this worksheet to a new workbook.
            Call wks.Copy(newWkb.Worksheets(1))

            'Get the reference to the copy of this worksheet and paste
            'all its content as values.
            Set newWks = newWkb.Worksheets(wks.Name)

        End If

    Next
    'ActiveWorkbook.SaveCopyAs Filename:=ThisWorkbook.Path & Format(Now(), "YYYYMMDD") & " Forecasting"& ".xlsm"
         Application.DisplayAlerts = False
     ActiveWorkbook.ActiveSheet.Name = "report"
     ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\"& "report "& Format(CStr(Now()), "dd-mmm (hh.mm.ss AM/PM)") & ".xlsx"
        Application.DisplayAlerts = False
        ActiveWorkbook.Close SaveChanges:=True


    End Sub


Viewing all articles
Browse latest Browse all 90065

Trending Articles