Solved

Is this even possible?

Posted on 2007-03-27
4
205 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:josepharichard
  • 2
4 Comments
 
LVL 8

Expert Comment

by:stochastic
Comment Utility
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
 
LVL 13

Accepted Solution

by:
WJReid earned 500 total points
Comment Utility
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
 
LVL 13

Assisted Solution

by:WJReid
WJReid earned 500 total points
Comment Utility
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
 

Author Comment

by:josepharichard
Comment Utility
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now