Consulting

Results 1 to 6 of 6

Thread: Help with VBA Macro

  1. #1

    Exclamation Help with VBA Macro

    Hi all,
    apologises in advance for any mistakes but i am new here!

    I have been tasked to create a macro which saves the current workbook and make a copy of it to another directory but I need to do automatically from Monday - Friday only?

    Could someone point me in the correct direction to do so? I have figured out how to do this on a specific time but I am not sure how to incorporate the Mon-Fri only time frame?

    Kindest,
    Sunny

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    You mean any time that anyone saves the WB on a Mon-Fri a copy gets saved to another folder?

    What is there's already a saved copy there?

    Or you can use Weekday()

    Option Explicit
    
    
    Sub DayCheck()
        
        If Weekday(Now) >= vbMonday And vbFriday <= Weekday(Now) Then
            MsgBox "OK to save"
        Else
            MsgBox "No save for you today"
        End If
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Hi Paul,

    Thank you so much for replying.

    I need the Macro to run automatically every day from Monday - Friday at X time.

    This macro would copy this workbook to a different location and if there is a file there already it should not replace this file it should just make additional copies with a new file naming pattern.

    Kindest,
    Sunny

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    This assumes the workbook will be open at TimeToSave every week day
    Option Explicit
    
    Public Sub SaveOnTime()
        'Runs CopyBookToFolder at SaveTime
        Application.OnTime SaveTime, "CopyBookToFolder"
    End Sub
    
    Private Function SaveTime()
    'Only called monday thru Friday
    'Sets the date and time to save
    
    Const TimeToSave = "16:30:00" 'Edit time to suit. Required to prevent Time part from creeping
    Dim DayToSave As Date
    
    'Moves Day to tommorow, except On Friday, moves it to Monday
        If Weekday(Date) >= vbMonday And Weekday(Date) <= vbThursday Then
            DayToSave = DateValue(DateAdd("d", 1, Date))
        Else
            DayToSave = DateValue(DateAdd("d", 3, Date))
        End If
    SaveTime = DayToSave + TimeValue(TimeToSave)
    End Function
    
    Private Sub CopyBookToFolder()
        'Edit Path and Extension to suit. Adds Date (Saved) stamp to book Name
        Me.SaveCopyAs "C:\TEMP\" & Me.Name & " - " & Format(Date, "yyyy/mm/dd") & "xlsm"
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    A macro only runs if Excel is open

    You could write a Windows Scripting Host (.VBS) script to copy the Excel file with a filename that includes the timestamp

    Add it as a scheduled task to run on x time Mon thru Fir
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Just wanted to say thank SamT and Paul for your help on this.
    You guys have been such a great help in my limited requeiments.
    Your assistance hit the nail on the head and I ironed out a few changes and additional things.
    Really appreciate it!!!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •