+ Reply to Thread
Results 1 to 7 of 7

Need help for VBA code - Date selection

  1. #1
    Registered User
    Join Date
    10-31-2014
    Location
    canada
    MS-Off Ver
    2007
    Posts
    64

    Need help for VBA code - Date selection

    Hi;

    I need to copy last work day's data into my current worksheet. Last work day's data can be found at K:\Downloads_Uploads\Downloads\Daily\GV8 downloads\2015\December\1214.xlsx. I know the code that open the file, copy and paste the data, but the difficulties I met are that how can I define the variables and create the path as year, month and date changes. (for example, today is Dec 15, so I need to open file from K:\Downloads_Uploads\Downloads\Daily\GV8 downloads\2015\December\1214.xlsx, but if today is Monday, Dec 21, then I need to open file K:\Downloads_Uploads\Downloads\Daily\GV8 downloads\2015\December\1218.xlsx).

    I appreciate if anyone can give me a help.

    Chris

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,855

    Re: Need help for VBA code - Date selection

    How about creating variables for year, month and day and have them created using Input Boxes? Then substitute the variable for the hard coded year, month and date in your path.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Need help for VBA code - Date selection

    Hi pmchris,

    See the following two macros for two slightly different alteratives.
    a.The first creates a 'Target String' and replaces tokens with the appropriate Year, Month, and Day of Month components.
    b. The second adds Year, Month, and Day of Month components to a 'Base String'.
    Please Login or Register  to view this content.
    Lewis

  4. #4
    Registered User
    Join Date
    10-31-2014
    Location
    canada
    MS-Off Ver
    2007
    Posts
    64

    Re: Need help for VBA code - Date selection

    Hi Lewis/Alan;

    I really appreciate you create these codes/suggestions for me. Since I am a beginner for using VBA code, there are some sentences or definitions I don't understand in your codes. For what I need, my thought is I want to create two dates in my current worksheet (for example, Cell A1 is today's date and Cell B1 is Last_work_day's date (no matter last work day is Friday, or any workday I worked on)), and then when I look for last work day's file in the folder, I can create the path by applying the variable I defined in Cell B1. At the end of code, I copy the Cell A1, and paste special value to Cell B1, so the Cell B1 is always the last work day's date. Does it make sense to create code as what I thought?

    I don't know how to define and apply variable in Cell B1.

    Chris

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need help for VBA code - Date selection

    Does this help?

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    10-31-2014
    Location
    canada
    MS-Off Ver
    2007
    Posts
    64

    Re: Need help for VBA code - Date selection

    Hi John;

    Your code works great. Thanks for help. Have a happy holiday.

    Chris

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need help for VBA code - Date selection

    You're welcome. Glad to help out and thanks for the feedback. Please refer to the link below under General Suggestions and mark this thread as SOLVED.

    http://www.excelforum.com/forum-rule...rum-rules.html

+ 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. Replies: 0
    Last Post: 11-16-2015, 08:57 AM
  2. [SOLVED] VBA copy paste a selection then find the current date and copy this selection row 61
    By woocaro in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-30-2015, 11:07 AM
  3. Start Date and End Date selection for chart.
    By DauntlessDS in forum Excel Charting & Pivots
    Replies: 17
    Last Post: 02-09-2015, 05:57 PM
  4. [SOLVED] Code to put today's date in empty cell dependent on selection from drop-down list
    By vickyh1 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 08-28-2013, 09:09 AM
  5. List Box Selection / Highligh Selection Code
    By Dr.SUN in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-28-2010, 11:48 PM
  6. Selection for Code
    By Jonathan Milla in forum Excel General
    Replies: 4
    Last Post: 08-22-2006, 11:20 AM
  7. [SOLVED] Selection Code
    By bw in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-28-2005, 10:05 AM

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