Consulting

Results 1 to 4 of 4

Thread: Date problem

  1. #1
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location

    Date problem

    Hi All

    What im trying to do is use the code below to put a sheet name into a list box if the date in it is between now - 30 and now +7 but it seem to add any date greater than today, any ideas please?

    I should add this code is part of a larger case statement

    For Each ws In ActiveWorkbook.Worksheets
            If ws.Range("C10").Value >= Date - 30 And Range("C10").Value <= Date + 7 Then
            ListBox1.AddItem ws.Name
            End If
    Next

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Gibbo,

    Put the ws. qualifier before the second Range statement, like
    If ws.Range("C10").Value >= Date - 30 And ws.Range("C10").Value <= Date + 7 Then
    The way you have it, it is checking to see if the active sheet's C10 is less than date + 7. Should take care of it for you!

    Matt

  3. #3
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Cant believe i missed that but there you go, sometimes its the simple stuff that gets you

    Thanks for opening my eyes matt

  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Doesn't sound like it's resolving the date addition/subtraction. You can force it by using the DateAdd function

    If ws.Range("C10").Value >= DateAdd("d", -30, Date) And _
    Range("C10").Value <= DateAdd("d", 7, Date) Then
    Ahh yes, well spotted
    K :-)

Posting Permissions

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