+ Reply to Thread
Results 1 to 8 of 8

Need VBA code to roll when I roll date in file

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2018
    Location
    Wichita, KS
    MS-Off Ver
    2016
    Posts
    4

    Need VBA code to roll when I roll date in file

    Hello all,

    I am working in Excel 2016. I have a VBA code that opens a worksheet and saves date from my current worksheet to the one I just opened. The issue is, I save my file with the VBA code every workday with a new date. I need the - Windows("Daily Position 180126.xlsm").Activate - part of the code to update when I save my new file every day. The code is below and I have highlighted the part I am having issues with.

    Sub Risk()
    '
    ' Risk Macro
    '

    '
    ChDir "\\ict0ms06\ntroacct\Trading\TSR Risk Reports\Consolidated Fertilizer"
    Workbooks.Open Filename:= _
    "\\ict0ms06\ntroacct\Trading\TSR Risk Reports\Consolidated Fertilizer\RiskCalcMasterNewReporting-NEW.xlsx"
    Sheets("Daily PNL").Select
    Windows("Daily Position 180126.xlsm").Activate
    Range("G22:H36").Select
    Selection.Copy
    Windows("RiskCalcMasterNewReporting-NEW.xlsx").Activate
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Sheets("Send to TSR").Select
    Range("A1").Select
    Application.CutCopyMode = False
    ActiveWorkbook.Save
    ActiveWindow.Close
    Range("A1").Select
    End Sub

    So tomorrow I need the red highlighted part of my code to say - Windows("Daily Position 180129.xlsm").Activate - when I roll and save my new file with that name.

    Thanks in advance!

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Need VBA code to roll when I roll date in file

    See if this works for you
    ChDir "\\ict0ms06\ntroacct\Trading\TSR Risk Reports\Consolidated Fertilizer"
    Dim dt As Variant
     Workbooks.Open Filename:= _
     "\\ict0ms06\ntroacct\Trading\TSR Risk Reports\Consolidated Fertilizer\RiskCalcMasterNewReporting-NEW.xlsx"
     Sheets("Daily PNL").Select
    dt = Format(Date, "yymmdd")
    Windows("Daily Position " & dt & ".xlsm").Activate
     Range("G22:H36").Select
     Selection.Copy
     Windows("RiskCalcMasterNewReporting-NEW.xlsx").Activate
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
     :=False, Transpose:=False
     Sheets("Send to TSR").Select
     Range("A1").Select
     Application.CutCopyMode = False
     ActiveWorkbook.Save
     ActiveWindow.Close
     Range("A1").Select
     End Sub
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Registered User
    Join Date
    01-26-2018
    Location
    Wichita, KS
    MS-Off Ver
    2016
    Posts
    4

    Re: Need VBA code to roll when I roll date in file

    The issue with that is I am always reporting on a day behind schedule. So my file will not be the same as the current day. Is there a way to reference a cell in the worksheet?

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Need VBA code to roll when I roll date in file

    Quote Originally Posted by scrivenk View Post
    The issue with that is I am always reporting on a day behind schedule. So my file will not be the same as the current day. Is there a way to reference a cell in the worksheet?
    Would
    dt = Format(Date - 1, "yymmdd")
    not work? If Not, and you want to use a range for the date, then just substitute the range reference for 'Date' and enter your date in short date format in that range.
    Last edited by JLGWhiz; 01-29-2018 at 06:38 PM.

  5. #5
    Registered User
    Join Date
    01-26-2018
    Location
    Wichita, KS
    MS-Off Ver
    2016
    Posts
    4

    Re: Need VBA code to roll when I roll date in file

    This wouldn't work. Today's date was the 29th, but I was reporting for Friday which was the 26th. So I guess said a better way is I am always reporting one workday behind.

  6. #6
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Need VBA code to roll when I roll date in file

    Quote Originally Posted by scrivenk View Post
    This wouldn't work. Today's date was the 29th, but I was reporting for Friday which was the 26th. So I guess said a better way is I am always reporting one workday behind.
    To use a range where you enter the date for the report, assume Range("A1")
    Change this line
    dt = Format(Date, "yymmdd")
    To
    dt = Format(Range("A1").Value, "yymmdd")
    For a different cell, just change the A1 to whatever cell you use.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need date calculation for pay roll Bi weekly
    By dotnetsupt in forum Excel General
    Replies: 1
    Last Post: 10-15-2017, 02:06 PM
  2. Real Estate Rent Roll Question - Return a value from a date range
    By TannerJQ8 in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 12-20-2015, 07:35 PM
  3. [SOLVED] Automatically Roll Date up to Start of Next Pay Period (Bi-weekly Pay Periods)
    By cmkarnes in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-21-2015, 11:23 AM
  4. Help! Automatically roll out a series of date by adjusting the duration
    By cli881231 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-15-2015, 03:58 PM
  5. [SOLVED] Need To Add time in Column up to 24hr then roll over the date
    By ValleyJ in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-14-2013, 08:08 PM
  6. Can you make the data roll with the date??
    By ed2339 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-30-2013, 12:46 PM
  7. Replies: 4
    Last Post: 10-01-2010, 08:14 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1