Setting date variable, so I can open files in dated folders using a macro

  • I'm trying to set a date variable (example 8/21/2016) so I can open a file using a date.


    Every day I have folders named for the previous day so for example today I have a date labeled 8/24/2016 to pull and process some reports, I have the macros set up nicely but I have to change the following code in the macro to reflect the date:


    Code
    Workbooks.Open Filename:="C:\Users\USERNAME\Desktop\8-24-2016\FILENAME.xls"


    Code
    ActiveWorkbook.SaveAs Filename:="C:\Users\USERNAME\Desktop\8-24-2016 Out\FILENAME.xlsm" _
        , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False


    What I'd like to be able to do instead of having to change both date sections is to have 1 section that I change and it impacts both dates for the open and save part of the macro. I've tried using:



    Code
    Dim dteProcess As Date
    Set dteProcess = '8/24/2016'
    
    
    Workbooks.Open Filename:="C:\Users\mearle\Desktop\(dteProcess)\act vs theo.xls"


    With no avail, however this seems to be what google search is suggesting.


    Can anyone point me in the correct direction on how to do this?

  • Re: Setting date variable, so I can open files in dated folders using a macro


    Code
    Dim dteProcess As String
      'dteProcess = "8-24-2016"
      dteProcess = Format(Date, "m-d-yyyy")
     
      Workbooks.Open Filename:="C:\Users\mearle\Desktop\" & dteProcess & "\act vs theo.xls"
  • Re: Setting date variable, so I can open files in dated folders using a macro


    Quote from Kenneth Hobson;776326
    Code
    Dim dteProcess As String
      'dteProcess = "8-24-2016"
      dteProcess = Format(Date, "m-d-yyyy")
     
      Workbooks.Open Filename:="C:\Users\mearle\Desktop\" & dteProcess & "\act vs theo.xls"


    This seems to be pulling in the current date regaurdless of what dteprocess= is set to.


    Edit figured it out, I had to move dteprocess= to after the format set.


    Edit2: This worked like a dream, thank you so much! This is going to save me tons and tons of time on having to scroll top to bottom just to change 2 dates for the input and output. I'd donate to you if I were able at this time, but again thank you!

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!