Link to home
Start Free TrialLog in
Avatar of josepharichard
josepharichard

asked on

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
Avatar of stochastic
stochastic
Flag of India image

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
ASKER CERTIFIED SOLUTION
Avatar of WJReid
WJReid
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of josepharichard
josepharichard

ASKER

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