Results 1 to 28 of 28

Thread: [RESOLVED] DateDiff

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Resolved [RESOLVED] DateDiff

    Can someone help with this? It doesn't error, but it doesn't work either.

    1. I don't think it's cycling through the cells, but I don't know how to make it.
    2. It doesn't work
    It should look at the date, compare with current and set the Font.


    Code:
          Sub DateText()
    
               Dim ws As Worksheet, dCell As Range
               Set ws = Sheets("LD")
               Set dCell = ws.Range("B6:B82")
               '- the number of days between today and the doc date
               '- NOTE: the "d" tells the DateDiff function to return the difference
               '- in days
               numOfDays = DateDiff("d", dToday, dCell.Text)
               '- with blocks simply save typing... here it applies everywhere you see
               '- .Font (it really means dCell.Font.ColorIndex = 3
               With dCell.Font
               '- i think the select statement is faster than a large if statement
               Select Case (numOfDays)
               '- if the doc date is within a week of today...
               '- Two Weeks
               Case Is <= 14
               '- ... yellow
               .ColorIndex = 6
               '-One Week
               Case Is <= 7
               '- ... Red
               .ColorIndex = 3
    
           End Select
    
           End With
    
          'On Error GoTo 0
    
           End Sub

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: DateDiff

    Code:
     numOfDays = DateDiff("d", dToday, dCell.Text)
    I think this just takes the text of first cell in the range.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DateDiff

    as far as i can see here dtoday has no value of any type, it may have been declared and set elsewhere, which is fine as long as it is still in scope

    Set dCell = ws.Range("B6:B82")
    this is a range of cells so as pradeep says you can not compare a range with differing values in one go

    try like
    vb Code:
    1. for each dcell in ws.range("b6:b82")
    2. ' your comparing, colouring code
    3. next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    as far as i can see here dtoday has no value of any type, it may have been declared and set elsewhere, which is fine as long as it is still in scope
    I thought it was a "Built In" name representing todays date internally.
    Maybe like
    numOfDays = DateDiff("d", dToday(), dCell.Text)
    But I got this code from a Resolved thread, and they didn't have anything like
    Dim dToday = dToday()
    listed anywhere. So I don't know.

    Code:
    for each dcell in ws.range("b6:b82")
    
    ' your comparing, colouring code
    next
    Well, I'm not comparing the colouring..
    I'm trying to:
    1. dcells contains dates 10/22/09
    2. DateDiff is supposedly to compare the date in the Cell with the Internal Computer Date.?.? And bring back the difference in "d"Days.
    Then the Case statement is supposed to "Set" the Cell "Text" Colour.
    Basically I'm trying to build an alarm system: Yellow= "Deadline is Approaching" Red= "You have a Week" kinda thing.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DateDiff

    I thought it was a "Built In" name representing todays date internally.
    date or now are built in functions to return the current date or date/time

    Well, I'm not comparing the colouring..
    well read that as
    your code to compare the dates and set the colouring
    that is the code you posted before put inside the loop, you can then see if you get a correct result or if there is some other issues as well
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: DateDiff

    Quote Originally Posted by tome10 View Post
    I thought it was a "Built In" name representing todays date internally.
    Maybe like

    But I got this code from a Resolved thread, and they didn't have anything like
    Dim dToday = dToday()
    listed anywhere. So I don't know.
    If you need current date, you can use the Date() function.
    e.g.
    numOfDays = DateDiff("d", Date(), dCell.Text)

    Quote Originally Posted by tome10 View Post
    Code:
    for each dell in ws.range("b6:b82")
    
    next
    Well, I'm not comparing the colouring..
    I'm trying to:
    1. dcells contains dates 10/22/09
    2. DateDiff is supposedly to compare the date in the Cell with the Internal Computer Date.?.? And bring back the difference in "d"Days.
    Then the Case statement is supposed to "Set" the Cell "Text" Colour.
    Basically I'm trying to build an alarm system: Yellow= "Deadline is Approaching" Red= "You have a Week" kinda thing.
    I think he meant like this:
    vb Code:
    1. Sub DateText()
    2.  
    3.            Dim ws As Worksheet, dCell As Range
    4.            Set ws = Sheets("LD")
    5.     For Each dCell in ws.Range("b6:b82")
    6.  
    7.            ' Set dCell = ws.Range("B6:B82")
    8.            '- the number of days between today and the doc date
    9.            '- NOTE: the "d" tells the DateDiff function to return the difference
    10.            '- in days
    11.            numOfDays = DateDiff("d", Date(), dCell.Text)
    12.            '- with blocks simply save typing... here it applies everywhere you see
    13.            '- .Font (it really means dCell.Font.ColorIndex = 3
    14.            With dCell.Font
    15.            '- i think the select statement is faster than a large if statement
    16.            Select Case (numOfDays)
    17.            '- if the doc date is within a week of today...
    18.            '- Two Weeks
    19.            Case Is <= 14
    20.            '- ... yellow
    21.            .ColorIndex = 6
    22.            '-One Week
    23.            Case Is <= 7
    24.            '- ... Red
    25.            .ColorIndex = 3
    26.  
    27.        End Select
    28.  
    29.        End With
    30.     Next
    31.  
    32.       'On Error GoTo 0
    33.  
    34.        End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: DateDiff

    Do you want to do this in VBA or through formulas?

    You can achieve what you want by using Formulas + use of conditional formatting

    Going by your first post (Range is B6:B82), Put this formula in C6 if it is empty and drag it down till C82...

    =DATEDIF(B6,TODAY(),"d")

    For the Conditional formatting, check my signature...
    Last edited by Siddharth Rout; Oct 10th, 2009 at 06:07 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    I need it in VBA Sid.
    This works.. Except for the case statements
    They should be:
    Case 0 To 7
    Case 8 to 14
    Also, what would be the best way to set the Range("A:I").Font
    Clarify, Instead of just the dCell text coloured I would like all text in the same row "Except for Col A".

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DateDiff

    vb Code:
    1. dcell.entirerow.font.colorindex = 3
    2. dcell.offset(0,-1).font.colorindex = -4105  ' default

    or better
    vb Code:
    1. range(dcell,dcell.offset(0,7)).font.colorindex = 3
    Last edited by westconn1; Oct 11th, 2009 at 01:38 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    This is basically the same thing but different range and I am setting the Interrior Color Index. I can't figure out why it won't work. I think it is the Case statement, but I've tried a few different ways and noting.

    Code:
    Private Sub ProjStat()
            Dim ws As Worksheet, psCell As Range
            Set ws = Sheets("LD")
            For Each psCell In ws.Range("C6:C82")
    
            pStat = psCell.Text
    
            With Range(psCell, psCell.Offset(-1, 6)).Interior
    
            Select Case (pStat)
            Case "Pan"
            .ColorIndex = 1
            Case "Decom"
            .ColorIndex = 2
            Case Is = "Hot"
            .ColorIndex = 3
            Case "Complete"
            .ColorIndex = 4
            End Select
            End With
            Next
            'On Error GoTo 0
        End Sub

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DateDiff

    i can not see any problem with the code
    what happens?
    are you sure the cells contain nothing except the what is in the case statements
    you could add a case else for testing

    vb Code:
    1. Select Case (pStat)
    2.         Case "Pan"
    3.         .ColorIndex = 1
    4.         Case "Decom"
    5.         .ColorIndex = 2
    6.         Case Is = "Hot"
    7.         .ColorIndex = 3
    8.         Case "Complete"
    9.         .ColorIndex = 4
    10.         case else
    11.             msgbox pstat
    12.         End Select
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    I got it thanks to the Else MsgBox. I had the words in the Cell CAPS, and the Case Non Caps.
    I Can't figure out how to color the cell to the left of the psCell.
    Code:
    With Range(psCell, psCell.Offset(0, 6)).Interior
    The 0 counts the Rows. Don't want that.
    The 6 Counts 6 to the right.
    How do you do Offset Left -2
    I need ("A:I")



    Also,
    It looks like I need an If or Case statement that takes Precedent over This Case Statement.
    It has to look at the Date Range ColB/-1 and If "" "Blank" color the interior something else. Disregard the other Case Statements.

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DateDiff

    you would have to do 2 offsets

    range(pscell.offset(,-2),pscell.offset(,6)).interior
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    This is my best shot.

    Code:
        Private Sub ProjStat()
            Dim ws As Worksheet, psCell As Range
            Set ws = Sheets("LD")
            For Each psCell In ws.Range("C6:C82")
    
            dStat = psCell.Offset(0, -1).Value
            pStat = psCell.Value
            
            If dStat <> "" Then 'This looks like an If to me
            With Range(psCell, psCell.Offset(0, 6)).Interior
            'Date:Green4, NoDate:Yellow6,
            Select Case (dStat)
            Case Is <> ""
            .ColorIndex = 4
            Case Is ""
            .ColorIndex = 6
            Case Else
                MsgBox dStat
            End Select
            
            Else: Select Case pStat 'Errors here Else without If
            'Pan:Blue8, Decom:Plum39, Hot:Red3
            Case "PAN"
            .ColorIndex = 8
            Case "DECOM"
            .ColorIndex = 39
            Case "HOT"
            .ColorIndex = 3
            Case "COMPLETE"
            .ColorIndex = 35
            Case Else
                MsgBox pStat
            End Select
            End With
            End If
            Next
            'On Error GoTo 0
        End Sub
    Last edited by tome10; Oct 11th, 2009 at 09:23 PM.

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DateDiff

    your end with is in a different code block to the with, one in if the other in else
    either move right outside the if/else code or have one for each
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    your end with is in a different code block to the with, one in if the other in else
    I thought I was "Sharing" the With between the Two CodeBlocks. I actually thought that was just "One" code block because The IF and Else go together but I guess that's not the case.
    either move right outside the if/else code or have one for each
    What do you mean Move "Right" Outside?
    Like this?
    Code:
    With Range(psCell, psCell.Offset(0, 6)).Interior
    If dStat <> "" Then 'This looks like an If to me
    Last edited by tome10; Oct 12th, 2009 at 01:51 AM.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    Alright, new train of thought. I am going to attempt to combine these 2 codes since they are so similiar.

    Code:
    Private Sub ProjStat()
            Dim ws As Worksheet, psCell As Range
            Set ws = Sheets("LD")
            For Each psCell In ws.Range("C6:C82")
    
            pStat = psCell.Text
    
            With Range(psCell.Offset(0, -2), psCell.Offset(0, 6)).Interior
    
            Select Case (pStat)
            'Pan:Blue8, Decom:Plum39, Hot:Red3
            Case "PAN"
            .ColorIndex = 8
            Case "DECOM"
            .ColorIndex = 39
            Case Is = "HOT"
            .ColorIndex = 3
            Case "COMPLETE"
            .ColorIndex = 4
            End Select
            End With
            Next
            'On Error GoTo 0
        End Sub
    Code:
        Private Sub ProjDate()
            Dim ws As Worksheet, dCell As Range
            Set ws = Sheets("LD")
            For Each dCell In ws.Range("B6:B82")
            numOfDays = DateDiff("d", Date, dCell.Value)
            With Range(dCell, dCell.Offset(0, 7)).Font
            Select Case (numOfDays)
            '-5-10 Days
            Case 5 To 10
            '-Yellow6
            .ColorIndex = 6
            '1-4 Days
            Case 1 To 4
            '-Red3
            .ColorIndex = 3
            Case Is <= 0
            '-Red3
            .ColorIndex = 3
    '        Case Else
    '            MsgBox pStat
            End Select
            End With
            Next
            'On Error GoTo 0
        End Sub

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DateDiff

    you can put the for each in the second sub immediately after the next in the first sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    I combined the 2 codes. But I have an issue.
    This code does the operations seperately.
    It seems I need to be able to compare the 2 Ranges at the same time.
    Is there a way to set the pCell, and the dCell Whereas dCell is just the Offset 0,-1.Value? For use as a Case dCell<>"" and pCell "Something"
    I can use Case "this" And "that" can't I? Or is "And" an If thing?
    Code:
            Private Sub ProjStat()
            Dim ws As Worksheet, psCell As Range, dCell As Range
            Set ws = Sheets("LD")
            For Each psCell In ws.Range("C6:C82")
            pStat = psCell.Text
            With Range(psCell.Offset(0, -2), psCell.Offset(0, 6)).Interior
            Select Case (pStat)
            'Pan:Blue8,Decom:Plum39,Hot:Red3,"":Green4,Complete:Purp/Gray47
            Case "PAN"
            .ColorIndex = 8
            Case "DECOM"
            .ColorIndex = 39
            Case Is = "HOT"
            .ColorIndex = 3
            Case "COMPLETE"
            .ColorIndex = 47
            Case ""
            .ColorIndex = 4
    '        Case Else
    '            MsgBox pStat
            End Select
            End With
            Next
            'On Error GoTo 0
            For Each dCell In ws.Range("B6:B82")
            numOfDays = DateDiff("d", Date, dCell.Value)
            With Range(dCell, dCell.Offset(0, 7)).Font
            Select Case (numOfDays)
            '5-10:Yellow6,1-4:Red3,<=0:Red3,"":Green4,
            Case Is >= 11
            .ColorIndex = 0
            Case 5 To 10
            .ColorIndex = 6
            Case 1 To 4
            .ColorIndex = 3
            Case Is <= 0
            .ColorIndex = 3
    '        Case Else
    '            MsgBox dStat
            End Select
            End With
            Next
            'On Error GoTo 0
        End Sub

  20. #20
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DateDiff

    try like this then, but as you still have to have 2 select case it makes only small difference
    vb Code:
    1. Private Sub ProjStat()
    2.         Dim ws As Worksheet, psCell As Range, dCell As Range
    3.         Set ws = Sheets("LD")
    4.         For Each dCell In ws.Range("B6:B82")
    5.         numOfDays = DateDiff("d", Date, dCell.Value)
    6.  
    7.         Select Case (numOfDays)
    8.         '5-10:Yellow6,1-4:Red3,<=0:Red3,"":Green4,
    9.         Case Is >= 11
    10.         fcol= 0
    11.         Case 5 To 10
    12.         fcol= 6
    13.         Case 1 To 4
    14.         fcol= 3
    15.         Case Is <= 0
    16.         fcol= 3
    17. '        Case Else
    18. '            MsgBox dStat
    19.         End Select
    20.         select case dcell.offset(,1)
    21.         Case "PAN"
    22.         icol= 8
    23.         Case "DECOM"
    24.         icol= 39
    25.         Case Is = "HOT"
    26.         icol= 3
    27.         Case "COMPLETE"
    28.         icol= 47
    29.         Case ""
    30.         icol= 4
    31.         end select
    32.         Next
    33.        with Range(dCell, dCell.Offset(0, 7))
    34.        .font.colorindex = fcol
    35.        .interior.colorindex = icol
    36.        end with

    not tested, may need minor alterations
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  21. #21
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: DateDiff

    @pete, I know you said "not tested" but not easy for the OP to figure out: the "With" block needs to be moved to inside the ForNext block.

    Foreground and Background colors need to be chosen carefully. They should not share a same value and should be easy to read.
    Code:
    Private Sub ProjStat()
        Dim dCell As Range
        Dim fcol As Long, icol As Long
        
        For Each dCell In Sheets("LD").Range("B6:B82")
            Select Case DateDiff("d", Date, dCell.Value)
                Case Is >= 11:    fcol = 3  '0
                Case Is >= 5:     fcol = 5  '6 '-- 5 to 10
                Case Else:        fcol = 10 '3 '-- < 5
            End Select
            Select Case dCell.Offset(0, 1)
                Case "PAN":       icol = 40 '34 '8
                Case "DECOM":     icol = 6  '35 '39
                Case "HOT":       icol = 44 '36 '3
                Case "COMPLETE":  icol = 34 '37 '47
                Case Else:        icol = 37 '40 '4
            End Select
            With dCell.Resize(1, 7) '-- this is the same but much simpler than
                                    '   Sheets("LD").Range(dcell, dcell.offset(0,7))
                .Font.ColorIndex = fcol
                .Interior.ColorIndex = icol
            End With
        Next
    End Sub
    Learn how to use proper code indents that will be easier to read and less errors.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    Anhn,
    Foreground and Background colors need to be chosen carefully. They should not share a same value and should be easy to read.
    That's what I'm trying to figure out right now.
    I need to go like
    Case dCell<>"" And pCell ="Pan" Color something
    Case dcell="" And pCell="Pan" Color something else
    With dCell.Resize(1, 7) '-- this is the same but much simpler than
    Hmm.. I would look at that and think it was Resizing the Cell. Does the 1 represent the Column?

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    Can someone advise. If Case "this" And "that" is legit?
    And what this actually is doing?
    Code:
    With dCell.Resize(1, 7)

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    I just pasted Anhn's code in a module and it retained the formatting. Did someone change something?

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    I can't figure out what the 1 represents in this. It errors when I put a 0 or -1.
    Code:
    With dCell.Resize(1, 8)
    Also, I need the Fill Interior A:I right now it only goes B:I, and i'm not sure how to fix that.
    Also, the Font Color Syntax can't share the same range with the Interior Color Syntax as Column A has Blue Hyperlinks.
    The if statement I put in isn't working either.
    I need the IF because When Date is in alarm, and Stat is Hot I end up having Red Background, and Red Font. I need to change the Font to white in that situation. I'm just having a terrible time figuring it all out. I just need a bunch of If and Statements. Maybe because I always used embedded Formula's and I would Concatenate lots of If statements i'm just not getting the Case Statements. I was also thinking of formatting the Stat cell with a Windings Font when the date was in alarm. So should I abandon the Case statements? Maybe I just need sleep.

    Code:
    Private Sub ProjStat()
        Dim dCell As Range
        Dim fcol As Long, icol As Long
        
        For Each dCell In Sheets("LD").Range("B6:B82")
            Select Case DateDiff("d", Date, dCell.Value)
                '<=0:Red3,1-4:Red3,5-10:Yellow6,
                Case Is >= 11:    fcol = 0
                Case Is >= 5:     fcol = 6
                Case Is >= 0:     fcol = 3
                Case Is < 0:      fcol = 3
                Case Else:        fcol = 0
            End Select
            If dCell.Offset(0, 1) = "HOT" Then
            Select Case DateDiff("d", Date, dCell.Value)
                '<=0:White1,1-4:White1,5-10:Yellow6,
                Case Is >= 11:    fcol = 0
                Case Is >= 5:     fcol = 6
                Case Is >= 0:     fcol = 1
                Case Is < 0:      fcol = 1
                Case Else:        fcol = 0
            End Select
            End If
            Select Case dCell.Offset(0, 1)
                'Pan:Blue8,Decom:Plum39,Hot:Red3,"":Green4,Complete:Purp/Gray47
                Case "PAN":       icol = 8
                Case "DECOM":     icol = 39
                Case "HOT":       icol = 3
                Case "COMPLETE":  icol = 47
                Case Else:        icol = 4
            End Select
            With dCell.Resize(1, 8) '-- this is the same but much simpler than
                                    '   Sheets("LD").Range(dcell, dcell.offset(0,7))
                .Font.ColorIndex = fcol
                .Interior.ColorIndex = icol
            End With
        Next
    End Sub
    Last edited by tome10; Oct 14th, 2009 at 12:57 AM.

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    Please?

  27. #27
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DateDiff

    try like
    Case "HOT": icol = 3: fcol = 1
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: DateDiff

    Alright, i'm back. I have this working mostly, but I can't seem to get the Interrior Color macro to color the A column of the Rows. I never could figure out what dCell.Resize(1, 8) was doing.
    The font color needs to affect Col B:I
    The Interrior color needs to affect A:I



    Code:
     Sub ProjStat()
        
        Dim dCell As Range
        Dim fcol As Long, icol As Long
        'Set dCell = dCell              'dCell=Date Cell
        'Set sCell = dCell.Offset(0, 1) 'sCell=Status Cell
             
            For Each dCell In Sheets("LD").Range("B6:B82")
            If dCell.Offset(0, 1) = "PAN" Then
            Select Case DateDiff("d", Date, dCell.Value)
              '<=0:Red3,1-4:White2,5-10:Yellow6,BLACK0,
                Case Is >= 11:    fcol = 0
                Case Is >= 5:     fcol = 6
                Case Is >= 0:     fcol = 3
                Case Is < 0:      fcol = 3
            End Select
            End If
            If dCell.Offset(0, 1) = "DECOM" Then
            Select Case DateDiff("d", Date, dCell.Value)
              '<=0:Red3,1-4:White2,5-10:Yellow6,BLACK0,
                Case Is >= 11:    fcol = 0
                Case Is >= 5:     fcol = 6
                Case Is >= 0:     fcol = 3
                Case Is < 0:      fcol = 3
            End Select
            End If
            If dCell.Offset(0, 1) = "HOT" Then
            Select Case DateDiff("d", Date, dCell.Value)
              '<=0:Red3,1-4:White2,5-10:Yellow6,
                Case Is >= 11:    fcol = 0
                Case Is >= 5:     fcol = 6
                Case Is >= 0:     fcol = 2
                Case Is < 0:      fcol = 2
            End Select
            End If
            If dCell.Offset(0, 1) = "COMPLETE" Then
            Select Case DateDiff("d", Date, dCell.Value)
              '<=0:Red3,1-4:White2,5-10:Yellow6,
                Case Is >= 11:    fcol = 0
                Case Is >= 5:     fcol = 0
                Case Is >= 0:     fcol = 0
                Case Is < 0:      fcol = 0
            End Select
            End If
            If dCell.Offset(0, 1) = "" Then
            Select Case DateDiff("d", Date, dCell.Value)
                '<=0:Red3,1-4:Red3,5-10:Yellow6,>11:Black0,
                Case Is >= 11:    fcol = 0
                Case Is >= 5:     fcol = 6
                Case Is >= 0:     fcol = 3
                Case Is < 0:      fcol = 3
            End Select
            End If
            If dCell <> "" Then
            Select Case dCell.Offset(0, 1)
                'Pan:Blue8,Decom:Plum39,Hot:Red3,"":Green4,Complete:Gray15
                Case "PAN":       icol = 8
                Case "DECOM":     icol = 39
                Case "HOT":       icol = 3
                Case "COMPLETE":  icol = 15
                Case Else:        icol = 4
            End Select
            End If
            If dCell = "" Then
            Select Case dCell.Offset(0, 1)
                'Pan:Blue8,Decom:Plum39,Hot:Red3,"":Green4,Complete:Purp/Gray47,NoDates:Yellow6,
                Case "PAN":       icol = 8
                Case "DECOM":     icol = 39
                Case "HOT":       icol = 3
                Case "COMPLETE":  icol = 15
                Case Else:        icol = 6
            End Select
            End If
            With dCell.Resize(1, 8) '-- this is the same but much simpler than
                                    '   Sheets("LD").Range(dcell, dcell.offset(0,7))
                .Font.ColorIndex = fcol
                .Interior.ColorIndex = icol
            End With
        
        Next
    End Sub

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width