Is this even possible?

Is there a way to set the order by which a process searches a sheet for information? I.E, search from the bottom up as opposed to the top down?

I have code which searches sheets for a date and copies the row with that date to another sheet, it does it from the top down, so if there happen to be two rows with the same date on the same sheet, the first date it hits is the one it takes, then it moves to the next sheet, but what I need is for it to take the more recent entry, for instance if there were more than one date entered but each had different times, the second row with the same date would be the one with the more recent time.  So, if the search was conducted from the bottom  up, it would hit take the correct row needed.

This is my current code:

Private Sub Worksheet_Change(ByVal Target As Range)
          If Target.Address <> [G3].Address Then Exit Sub
          If Not IsDate(Target.Value) Then
               MsgBox "Value entered into " & Target.Address & " is not a date"
               Exit Sub
          End If
          If Target.Value > 0 Then
            Sheets("Hidden Data").Range("A1").Value = Target.Value
            Dim ws As Worksheet
            Dim fnd As Range
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            For Each ws In ThisWorkbook.Worksheets
                 If ws.Name <> "Status Report" And ws.Name <> "Hidden Data" Then
                      ws.Activate
                      Set fnd = ws.Range("A:A").Find(Target.Value, , xlValues, xlWhole)
                      If fnd Is Nothing Then
                        Sheets("Hidden Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Value = "'"
                      End If
                      If Not fnd Is Nothing Then
                        fnd.EntireRow.Select
                           fnd.EntireRow.Copy
                           Sheets("Hidden Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
                      End If
                 End If
            Next
          End If
          Sheets("Status Report").Activate
          Application.ScreenUpdating = True
          Application.EnableEvents = True
     End Sub

I tried messing with a code like:

Sheets("PDS (1)").Select
    Range("A2").Select
    Range("A1:H4").Sort Key1:=Range("A2"), Order1:=xlDescending, Key2:=Range( _
        "B2"), Order2:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase _
        :=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
        DataOption2:=xlSortNormal

To try to automatically change the sorting of the sheet before the process of searching the sheets was done, but couldn't find the correct way to get it implimented with the first bit of code.

Anyhow, first I just need to know if its possible at all, thus the lower points, will upgrade to 500 if someone can actually help me figure out how to get this to somehow work.  

The goal:  using first bit of code, grab not only the row with the correct date, but the most recent time as well.

Thank you all for your help.

Joe
josepharichardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stochasticCommented:
Joe,

Of course it is possible! There would be several ways to do this, including the sorting,
but in my opinion sorting is an overkill.

After you have found the target date, put a loop with the .findnext method to find
more records with the same date. Compare the time of the newly found records with
the latest time so far found, and if the new record is of a later time, just overwrite what
you pasted on the hidden sheet.

Let me know if you would like me to modify your code and post it here, but I thought
it would be more fun for you to get it working yourself!

- stochastic
0
WJReidCommented:
Hi Joe,

Can you put this formula in cell IV1 of each of the sheets, excluding the Hidden Data and Status Report Sheets:

=ROW(INDEX($A$1:$A$20000,SMALL(IF($A$1:$A$20000='Status Report'!$G$3,ROW($A$1:$A$20000)-ROW($A$1)+1,ROW($A$20000)+1),COUNTIF($A$1:$A$20000,'Status Report'!$G$3)),1))

This is an array formula, so it must be entered by clicking Ctrl+Shift+Enter together. You will know if it has entered correctly because there will be curly brackets around it. You can enter it in one sheet and just copy and paste to the others.

 Then try the following code:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim dRow As Double
    Dim dDate As Date
    If Target.Address <> [G3].Address Then Exit Sub
    If Not IsDate(Target.Value) Then
        MsgBox "Value entered into " & Target.Address & " is not a date"
        Exit Sub
    End If
    If Target.Value > 0 Then
        dDate = Range("G3").Value
        Sheets("Hidden Data").Range("A1").Value = Target.Value
        Dim ws As Worksheet
        Dim fnd As Range
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Status Report" And ws.Name <> "Hidden Data" Then
            ws.Activate
            If WorksheetFunction.CountIf(ws.Range("A1:A20000"), dDate) > 0 Then
                dRow = ws.Range("IV1").Value
                ws.Rows(dRow).EntireRow.Copy
                Sheets("Hidden Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
            Else
                Sheets("Hidden Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Value = "'"
            End If
        End If
        Next
    End If
    Sheets("Status Report").Activate
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub

Bill
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WJReidCommented:
Hi Joe,
To save you having to put the formulas in, just put this line:
   ws.Range("IV1").FormulaArray = _
      "=ROW(INDEX(R1C1:R20000C1,SMALL(IF(R1C1:R20000C1='Status Report'!R3C7,ROW(R1C1:R20000C1)-ROW(R1C1)+1,ROW(R20000C1)+1),COUNTIF(R1C1:R20000C1,'Status Report'!R3C7)),1))"

afer this line;
  ws.Activate

and it will put the formulas in for you.
Bill
0
josepharichardAuthor Commented:
Bill, yer a genius, thank you so much, finally, this book is done, and I must say I've learned quite a bit in the process..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.