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!![]()
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 theicon 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!)
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!
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.
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
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.
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.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
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 theicon 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!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks