+ Reply to Thread
Results 1 to 6 of 6

Improving the speed of a loop through a date range

  1. #1
    Registered User
    Join Date
    06-02-2015
    Location
    Oklahoma
    MS-Off Ver
    2010
    Posts
    8

    Improving the speed of a loop through a date range

    Hi,

    Basic problem:
    I want to loop through every minute of a date/time range as fast as possible.
    For example,
    Start Date: 7/9/15 8:00 PM
    End Date: 7/10/15 8:00 AM
    The code should go through 8:01 PM, 8:02 PM, 8:03 PM, etc. until the end date/time.

    Here's the context:
    I have a spreadsheet that pulls in values based on an entered date and time.
    Two cells perform calculations on those values and give that date/time an assigned number, either a 0 or 2.
    I want the macro to go through every minute in a date range and when the assigned numbers of the two cells do not match, to copy and paste the date in either column 1 or column 2.

    Here is the code I have:

    This works, but it iterates slowly, I can watch the minute increase when ideally I'd like it to be close to instantaneous.
    I tried looking into For...Next loops but didn't make much progress.

    Any help would be greatly appreciated!

    Please Login or Register  to view this content.
    Thanks

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Improving the speed of a loop through a date range

    Maybe:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Improving the speed of a loop through a date range

    Hi,

    Rather than use loops which are inherently slow and if there is standard functionality available you should use that.
    In this case I'd be inclined to use a standard IF function to populate the columns 1 or 2 with your dates.

    You could enlist the help of a macro to do this and add the function to the relevant block of cells in a single instruction. The big advantage is that you don't need a loop of any sort, either a DO loop or a FOR loop
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: Improving the speed of a loop through a date range

    A common way to speed up macros considerably is by adding these two lines of codes to the start and end of the macro respectively..

    Please Login or Register  to view this content.
    Hope this helps,
    - Stu

  5. #5
    Registered User
    Join Date
    06-02-2015
    Location
    Oklahoma
    MS-Off Ver
    2010
    Posts
    8

    Re: Improving the speed of a loop through a date range

    Thanks for all the input!

    xladept: That definitely makes the copying and pasting thing I had much simpler.
    Unfortunately the overall speed is the same. I wonder if it has more to do with the calculations its doing each time rather than looping through the date.

    StuCram: I did try that but it still takes the same amount of time

    Richard: I think the only problem with that is I don't know which dates needed to be copied and pasted over until I cycle through it. A piece of equipment can be assigned a number (0, 1, or 2) referring to (DOWN, SLOW, UP) at any given time based on calculated values. I'm interested in the transition from 0 to 2 (i.e. DOWN to UP or UP to DOWN). However, I don't know when that transition occurs unless I look at every specific time. But you bring up a good point about how I approach it; maybe I need to completely rethink how to gather the dates without having a loop.

    I'll keep working on it!
    Last edited by NorthUnit; 07-10-2015 at 05:15 PM.

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Improving the speed of a loop through a date range

    How are the O's calculated??

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

+ 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. How to select range of cells that equal certain value...need help improving code.
    By dbravo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-25-2014, 03:28 AM
  2. Improving a Template for Set Range = .Find
    By Doug Nguyen in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-06-2014, 09:55 PM
  3. Improving calculation speed of a sheet utilizing many "sumifs"
    By blademan88 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-10-2013, 02:21 PM
  4. Improving Speed of Index and Match Functions
    By hazza147 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-30-2010, 01:05 PM
  5. improving speed and efficiency
    By wishmaker in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-14-2010, 11:08 AM

Tags for this Thread

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