Consulting

Results 1 to 7 of 7

Thread: IN Statement in VBA

  1. #1
    VBAX Newbie
    Joined
    Feb 2016
    Posts
    5
    Location

    IN Statement in VBA

    Hi all

    I have a macro, which I use to update a number of different reports. For example monday I want to update report A & B, Tuesday report B & C, Wednesday report A etc. To do this I use the Weekday(Now(), vbMonday) to determine whether a report should be called for example:

    If Weekday(Now(), vbMonday) = 1 Or Weekday(Now(), vbMonday) = 3 Then Productions_Reports

    Is there any way I can write the weekday equals 1 or 3 in a smarter way ?

    For example using something similar to SQL Statement IN eg.
    If Weekday(Now(), vbMonday) IN ("1","3) Then Productions_Reports

    I look forward to your replies

    Br,
    Simon

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Have you tried Select Case?


    Select Case Weekday(Now(), vbMonday)
    Case 1, 3
    Productions_Reports
    Case Else
    'Do nothing
    End Select
    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 Newbie
    Joined
    Feb 2016
    Posts
    5
    Location
    Hi mark007

    The problem with that solution is I have reports I run every day, and some I run on specify days, so there is no clear pattern. For your solution I would need like ten cases, which is not better than I currently have.

    Br,
    SImon

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I'd use a bit mapping technique to have flexibility


    Option Explicit
    Enum RunDays
        rdSun = 2
        rdMon = 4
        rdTue = 8
        rdWed = 16
        rdThu = 32
        rdFri = 64
        rdSat = 128
    End Enum
    
    Sub RunReports()
        
        Report1 rdMon
        Report2 rdTue
        Report3 rdMon + rdTue
        Report4 rdSun
    End Sub
    
    
    'runs on Monday
    Sub Report1(RunOn As RunDays)
        If (2 ^ Weekday(Now) And RunOn) = 0 Then Exit Sub
        
        MsgBox "Report 1"
    End Sub
    
    
    'runs on Tuesday
    Sub Report2(RunOn As RunDays)
        If (2 ^ Weekday(Now) And RunOn) = 0 Then Exit Sub
        MsgBox "Report 2"
    End Sub
    
    'runs Monday and Tuesday
    Sub Report3(RunOn As RunDays)
        If (2 ^ Weekday(Now) And RunOn) = 0 Then Exit Sub
        MsgBox "Report 3"
    End Sub
    
    
    'runs Sunday
    Sub Report4(RunOn As RunDays)
        If (2 ^ Weekday(Now) And RunOn) = 0 Then Exit Sub
        MsgBox "Report 4"
    End Sub
    Last edited by Paul_Hossler; 03-13-2017 at 08:41 AM. Reason: tweaked to fix parens
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Newbie
    Joined
    Feb 2016
    Posts
    5
    Location
    Hi Paul

    Thank you for your answer

    However, I think that solution is more complicated and less readable than mine.

    Do you guys know if it is possible using arrays ?

    Br,
    Simon

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    even simpler:

    if instr("13",Weekday(date, 2)) Then Productions_Reports
    or
    if len(replace("13",weekday(date,2),""))=1 then Productions_Reports
    or using an array
    if ubound(filter(array(1,3),weekday(date,2)))=0 then Production_reports
    in Excel you can use

    if not iserrror(application.match(weekday(date,2),array(1,3),0)) then Productions_Reports
    Last edited by snb; 03-14-2017 at 12:58 AM.

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by shs91 View Post
    Hi Paul

    However, I think that solution is more complicated and less readable than mine.
    I'll disagree, esp since

    I have reports I run every day, and some I run on specify days, so there is no clear pattern

    implies that "Productions_Reports" is only one of several / many that are run on various schedules

    It appears to me that a better example would be something like

     If Weekday(Now(), vbMonday) = 1 Or Weekday(Now(), vbMonday) = 3 Then Productions_Reports_1
     If Weekday(Now(), vbMonday) = 2 Or Weekday(Now(), vbMonday) = 3 Then Inventory_Reports_2
     If Weekday(Now(), vbMonday) = 4 Or Weekday(Now(), vbMonday) = 5 Then Sales_Reports_3
     If Weekday(Now(), vbMonday) = 3 Or Weekday(Now(), vbMonday) = 6 Or Weekday(Now(), vbMonday) = 1 Or Weekday(Now(), vbMonday) = 4 Then Warehouse_Reports_4
    
    etc
    IN MY OPINION >>> I think that a bunch of IF's that that are more difficult to maintain and to read <<< IN MY OPINION



    One way to use arrays

    Option Explicit
    
    Sub UsingArray()
    
        Dim aReports(vbSunday To vbSaturday)
        Dim v As Variant
    
        aReports(vbMonday) = Array("Report1", "Report2")
        aReports(vbTuesday) = Array("Report1", "Report3")
        aReports(vbWednesday) = Array("Report2", "Report3")
        'etc
    
        For Each v In aReports(Weekday(Now))
            Application.Run v
        Next
    End Sub
    
    Sub Report1()
        MsgBox "Report 1"
    End Sub
    Sub Report2()
        MsgBox "Report 2"
    End Sub
    Sub Report3()
        MsgBox "Report 3"
    End Sub
    Sub Report4()
        MsgBox "Report 4"
    End Sub
    Sub Report5()
        MsgBox "Report 5"
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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