+ Reply to Thread
Results 1 to 5 of 5

Thread: Find the latest Date from one column across particular rows

  1. #1
    Registered User
    Join Date
    08-04-2009
    Location
    scotland
    MS-Off Ver
    Excel 2003
    Posts
    19

    Exclamation Find the latest Date from one column across particular rows

    Hi, I'm hoping someone can point me in the right VBA direction!

    I have rows of similar data:

    Group Process Scheduled Date Actual Date
    ENG_STRIP Engine Strip Process 17.06.2009 17.06.2009
    ENG_S_INSP Engine Strip Inspection 20.06.2009 20.06.2009
    MOD_STRIP Module Strip Process 18.06.2009 18.06.2009
    SENTENCE Sentence Process 01.07.2009 01.07.2009

    ... repeats with different dates ...

    I would like the macro to search down column B for all "sentence process" and return the latest corresponding "actual date" from column D.

    This date could then be displayed in any cell such as G1.

    Any help would be appreciated!

    Thanks!

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: Help ASAP! Find the latest Date from one column across particular rows

    Try this:
    Sub LastActual()
    Dim LR As Long, i As Long, Val As String
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
        For i = 2 To LR
            If Cells(i, "B") = "Sentence Process" Then _
                If Cells(i, "D") > Val Then Val = Cells(i, "D")
        Next i
    
    Range("G1") = Val
    MsgBox ("Updated in G1")
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    08-04-2009
    Location
    scotland
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Find the latest Date from one column across particular rows

    Thats superb! Thanks very much, however, I do have one further question ...

    If I want to do the same for other rows such as "Module Strip Process" how would I edit the code?

    I tried to copy and changed i to j & the LR to MS but it never worked. Sorry for my lack of knowledge!

  4. #4
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Find the latest Date from one column across particular rows

    Just on the off chance you're interested you can do the same without VBA:

    G1: =MAX(IF($B$1$:$B$100="Sentence Process",$D$1:$D$100))
    confirmed with CTRL + SHIFT + ENTER

    Even if you opt for VBA pending volume of data you may find running an Evaluation of the above to be quicker than iterating each row in the data set.

  5. #5
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: Find the latest Date from one column across particular rows

    LR just means "Last Row", so you shouldn't need to change that.

    "i" is just a progressive variable. Once you've gone through the FOR/NEXT loop completely for the "Sentence Process", you can just use the loop AS IS for the next value just changing the search to "Module Strip Process".

    The only things you need to edit are the search words and where you write it down when you're done.

    Sub LastActual()
    Dim LR As Long, i As Long, Val As String
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
        For i = 2 To LR
            If Cells(i, "B") = "Sentence Process" Then _
                If Cells(i, "D") > Val Then Val = Cells(i, "D")
        Next i
        Range("G1") = Val
    
        Val = 0
        For i = 2 To LR
            If Cells(i, "B") = "Module Strip Process" Then _
                If Cells(i, "D") > Val Then Val = Cells(i, "D")
        Next i
        Range("G2") = Val
    
    MsgBox ("Done")
    End Sub
    That's just to show you what you were trying to do. However that means you have loop through the range fully TWICE, and THREE times if you add another level. That's not efficient at all.

    If you're going to keep expanding, I'd expand my variables as well and store the answer for each one as we loop through the code only once, then write out all the variable at once.
    Sub LastActuals()
    Dim LR As Long, i As Long, test As String
    v1 As String, v2 As String, v3 As String
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
        For i = 2 To LR
            test = Cells(i, "B").Value
            Select Case test
                Case "Sentence Process"
                    If test > v1 Then v1 = test
                Case "Module Strip Process"
                    If test > v2 Then v2 = test
                Case "Engine Strip Process"
                    If test > v3 Then v3 = test
            End Select
        Next i
        
    Range("G1") = v1
    Range("G2") = v2
    Range("G3") = v3
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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.2.0