Consulting

Results 1 to 3 of 3

Thread: Solved: Adding Time Stipulation to VBA

  1. #1
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location

    Solved: Adding Time Stipulation to VBA

    I know I can use this code to add time constraints:
    'Runs a macro at 4:30 PM
    Application.OnTime TimeValue("16:30:00"), "Name_of_Macro"
    And I know you can use this code to run specific code depending on the day:
    '    Dim sDate As String
    '    sDate = Format(Date, "ddd")
    '    If sDate = "Sun" Then
    '        Run "MacroName"
    '    Else
    '        If sDate = "Mon" Then
    '            Run "MacroName"
    '        End If
    '    End If
    How can I combine the two and say something like:
    '    Dim sDate As String
    '    sDate = Format(Date, "ddd")
    '    If sDate = "Mon" and Windows.Time  < ("12:00:00") Then
         Run "MacroName"
    '    Else
    '        If sDate = "Mon" and Windows.Time > ("12:00:00") Then
    '            Run "MacroName"
    '        End If
    '    End If
    I want to be able to have one button on a Excel UserForm, and be able to run two different macro's depending on what time it is. I don't want to use the OnTime value mentioned above because the time varies, but what never varies is that one macro needs to be run before lunch, and the other macro needs to be run after lunch. How can I code it to do the above?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Not far away from it
    Try
    [VBA] Dim sDate As String
    sDate = Format(Date, "ddd")
    If sDate = "Mon" And Time < TimeValue("12:00:00") Then
    Run "MacroName"
    Else
    If sDate = "Mon" And Time > TimeValue("12:00:00") Then
    Run "MacroName"
    End If
    End If
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location
    That worked perfectly, thank yoU!!!

Posting Permissions

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