Avatar of M. Saad
M. Saad
 asked on

VBA to Update status Column based on recent date

Hello , I need your help please

I have very large database file about 100000 rows and 20 column , sample example as follow


Column A( Name)       Column B( Date)      Column C ( Status)
15600-DP-C-01452      4/10/2017      Superseded
15600-DP-C-01452      5/11/2017      Current
15600-DP-C-01452      1/25/2017      Superseded
15600-DP-C-01452      2/28/2017      Superseded
15600-DP-C-02345      3/07/2017      Superseded
15600-DP-C-02345      1/24/2017      Superseded
15600-DP-C-02345      1/12/2017      Superseded
15600-DP-C-02345      4/13/2017      Current

I need to have VBA code to look up the value in Column A and then search for the most recent date in Column B corresponding to this value , and then add the work “ current “ in the adjacent cell for this date and “ superseded” to the old date

Hope my request is clear
Rev_Status.xlsx
VBAMicrosoft OfficeMicrosoft Excel

Avatar of undefined
Last Comment
Martin Liss

8/22/2022 - Mon
Martin Liss

Sub Status()
Dim lngLastRow As Long
Dim lngRow As Long
Dim lngMR_Row As Long
Dim strOldName As String
Dim dteMostRecent As Date

Application.ScreenUpdating = False

lngLastRow = Range("A1048576").End(xlUp).Row
strOldName = Range("A2")
dteMostRecent = Range("B2") - 1
Range("C2:C" & lngLastRow) = "Superseded"

For lngRow = 2 To lngLastRow
    If Cells(lngRow, "A") = strOldName Then
        If DateDiff("d", dteMostRecent, Cells(lngRow, "B")) > 0 Then
            dteMostRecent = Cells(lngRow, "B")
            lngMR_Row = lngRow
        End If
    Else
        Cells(lngMR_Row, "C") = "Current"
        strOldName = Cells(lngRow, "A")
        lngMR_Row = lngRow
        dteMostRecent = Cells(lngRow, "B")
    End If
Next
Cells(lngMR_Row, "C") = "Current"

Application.ScreenUpdating = True

End Sub

Open in new window

M. Saad

ASKER
hello ,

thanks a lot for your prompt reply Martin

very good code and  working  for the test file
But when I enter new row for the same document no it doesn’t change the status automatically and I have to run the macro again and this take too much time since I have over than 900000 row.

Can you please make this code update faster ?
Martin Liss

When you add a new row, can it be anywhere in the sheet, or is it always at the bottom?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Martin Liss

Try this.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lngLastRow As Long
Dim lngRow As Long
Dim lngMax As Long
Dim rngVisible As Range
Dim dteMostRecent As Date
Dim lngMR_Row As Long

Application.ScreenUpdating = False

ActiveSheet.UsedRange.AutoFilter

lngLastRow = Range("A1048576").End(xlUp).Row

If Not Intersect(Target, Range("A2:B" & lngLastRow)) Is Nothing Then
    If Cells(Target.Row, "A") = Empty Or Cells(Target.Row, "B") = Empty Then
        Exit Sub
    Else
        Range("$A$1:$C$" & lngLastRow).AutoFilter Field:=1, Criteria1:=Cells(Target.Row, "A")
        Set rngVisible = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
        dteMostRecent = #1/1/1950#
        Application.EnableEvents = False
        For lngRow = ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 2).Row To rngVisible.End(xlDown).Row
            Cells(lngRow, "C") = "Superseded"
            If DateDiff("d", dteMostRecent, Cells(lngRow, "B")) > 0 Then
                dteMostRecent = Cells(lngRow, "B")
                lngMR_Row = lngRow
            End If
        Next
        Cells(lngMR_Row, "C") = "Current"
        Application.EnableEvents = True
    End If
End If

ActiveSheet.UsedRange.AutoFilter

Application.ScreenUpdating = True

Open in new window

M. Saad

ASKER
hello, i would insert row anywhere in the log, not necessary at last row.
i copied the second code to my file but its not working , i am not sure what is wrong.. can you please check it again ? thanks a lot.
Martin Liss

When I tested it it worked for me. What I did was to

  1. Right-click on an existing row number and selected Insert
  2. Copied the "Name" from the above row and pasted it into column "A" of the new row
  3. Entered a date into the "Date" column of the new row
  4. Selected any other cell

Did you do something different?

Can you attach your workbook or send it to me in an EE message.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
M. Saad

ASKER
sorry i was not clear , i have no problem to insert row in anywhere in the file, but i couldn't copy the second macro to my file.
i have attached copy of original file. sorry for disturbing you Martin about this but please note that :
Column A : Document Name
Column E: Rev. Status
Column M: Received_Date
start row : 6

thanks again
Rev_Status_Rev-1.xlsx
Martin Liss

Here you go.
29016587a.xlsm
M. Saad

ASKER
very good Martin, and this is what i really wish to have for my file, but please see below 2 points need your help with :

1 -  i set up filter in the header row ( row No 6) , and when i search for any document using filter and try to update revision , immediately filter will disappear , can you please fix it ?
2- i have moved all my data to the file ( more than 104385 rows ) and i have to click double click in each cell in the column E " Rev Status " in order to activate the macro and word " current " will appear for the most recent date. is it possible to make this process with other macro to refresh all data after update ?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Martin Liss

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
M. Saad

ASKER
hello, i have sent you my original file as message, would you please check it?

i am kindly asking you to keep the filter in the top of the table no matter any update happens in the log.
kind regards
Martin Liss

OK, I'll be back in a while.
Martin Liss

I sent you a message about a problem with the new workbook.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
M. Saad

ASKER
sorry for late reply , i have sent you reply . kindly check
M. Saad

ASKER
hello , i am very thankful for your great work , only few advise from you if possible, i have sent you reply . kindly check
M. Saad

ASKER
Dear Martin

i am speechless , this exactly what i wanted to have in my file  , working like charm. this will save a lot of time and efforts for my daily work, very happy indeed.

really appreciate your great efforts during all week and high ethics ,I am trying it since morning and its really good , everything is very good for me.

sample of final file is attached for further use by anybody looking for the same.

many thanks Martin for you and God bless you.

Kind regards
29016587f1.xlsm
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
M. Saad

ASKER
error
Martin Liss

You're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2016