+ Reply to Thread
Results 1 to 4 of 4

How to make a macro end with a date criteria

Hybrid View

Ronnet2 How to make a macro end with... 09-23-2015, 04:18 AM
Ronnet2 Re: How to make a macro end... 09-23-2015, 05:13 AM
Alf Re: How to make a macro end... 09-23-2015, 05:22 AM
Ronnet2 Re: How to make a macro end... 09-23-2015, 05:39 AM
  1. #1
    Registered User
    Join Date
    03-26-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    75

    How to make a macro end with a date criteria

    Hi,

    I've made the following macro with a loop.

    ----

    Sub Run_model_loop()

    For i = 2 To 150

    Value = i
    ThisWorkbook.Sheets("EVENTS").Calculate
    Sheets("SUB2").Rows(i - 1).Calculate
    Sheets("SUB2").Rows(i).Calculate
    Sheets("SUB3").Rows(i).Calculate
    Sheets("SUB4").Rows(i).Calculate

    Next i

    ThisWorkbook.Sheets("EVENTS").Calculate
    ThisWorkbook.Sheets("Order results").Calculate

    End Sub


    ----

    Right now the end trigger is when i = 150. However, the end trigger should be a specific end-date (cel B2 of sheet SYSTEM). I would like for the macro to check the date in column C (i.e. request date) of sheet SUB2. When it recalculates a row of SUB2, and the date in column C on that particular row >= the end date (i.e. SYSTEM!B2) then the macro should stop the loop.

    Is this possible?

    File: Event-Based Sim_(with loop macro).xlsm
    Last edited by Ronnet2; 09-23-2015 at 04:40 AM. Reason: Typo

  2. #2
    Registered User
    Join Date
    03-26-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    75

    Re: How to make a macro end with a date criteria

    I've given it another go. Now I've put in a conditional If/then line. It seems to accept the ''Cells(i,3)'' reference, moving the ''end sub'' line before the ''next i'' line. However, I'm getting a ''block if without end if'' error. How can I fix this?


    Sub Run_model_loop()

    ThisWorkbook.Sheets("EVENTS").Calculate

    For i = 2 To 150

    Value = i
    ThisWorkbook.Sheets("EVENTS").Calculate
    Sheets("SUB2").Rows(i - 1).Calculate
    Sheets("SUB2").Rows(i).Calculate
    Sheets("SUB3").Rows(i).Calculate
    Sheets("SUB4").Rows(i).Calculate

    If Sheets("SUB2").Cells(i, 3) >= Sheets("SYSTEM").Cells(2, 2) Then
    ThisWorkbook.Sheets("EVENTS").Calculate
    ThisWorkbook.Sheets("Order results").Calculate
    End Sub

    Else
    Next i

    ThisWorkbook.Sheets("EVENTS").Calculate
    ThisWorkbook.Sheets("Order results").Calculate

    End Sub

  3. #3
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,795

    Talking Re: How to make a macro end with a date criteria

    You have an example of a "For i =" loop stopped by a given date in the "AddDate" macro.

    Alf

  4. #4
    Registered User
    Join Date
    03-26-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    75

    Re: How to make a macro end with a date criteria

    Quote Originally Posted by Alf View Post
    You have an example of a "For i =" loop stopped by a given date in the "AddDate" macro.

    Alf
    The EVENT sheet generates random dates (i.e. request times) on which events occur.
    After each request time is generated, the event list is ranked based on this request time.
    On sheet SUB2 these events are brought in on the correct ranked order according to the request date.
    So the first time a request time (sheet SUB2 cell(3,i) >= to the end date (Sheet SYSTEM cell B2) then the macro should end.

    I can't give an example because the request times are randomly generated.

+ 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: 4
    Last Post: 09-16-2015, 08:07 AM
  2. [SOLVED] Make my macro run for all sheets not only in the same sheet with the same criteria
    By hazemrezo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-07-2015, 11:39 AM
  3. Replies: 6
    Last Post: 06-09-2015, 07:37 PM
  4. [SOLVED] Need IF statement or Macro to make decision based on multiple criteria
    By weddica in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-25-2013, 03:30 PM
  5. Can a macro make selections to slicers based on values or criteria in cells?
    By Yas420 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-26-2013, 03:07 PM
  6. Make a macro run based on three types of criteria
    By aarzu.maknojia in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-05-2012, 02:16 AM
  7. [SOLVED] how do you make a macro run automatically when a criteria is met?
    By Lee Crew in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-13-2006, 11:10 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