Search based on a condition

SOKOJENA

New Member
Joined
Jun 25, 2018
Messages
2
I have the following code to find the maximum and minimum time for a person and display the results. The problem is when l run the code its giving me results for everyone not a single person l want

Sub Saving()


Dim personname As String
Dim finalrow As Integer
Dim i As Integer
Dim newSheet As Worksheet
Dim yourWorksheet As Worksheet
Dim searchArea As Range
Dim searchResult As Range
Dim yourWorkbook As String
Dim rowMinimum As Long
Dim minimumValue As Date




yourWorkbook = "Saving.xlsm"
Set yourWorksheet = Workbooks(yourWorkbook).Worksheets("Sheet1")


personname = Range("D2").Value
finalrow = Range("A100").End(xlUp).Row




For i = 2 To finalrow
If Cells(i, 1) = personname Then


Set searchArea = yourWorksheet.Range(yourWorksheet.Cells(i, 3), _
yourWorksheet.Cells(yourWorksheet.Cells(i, 3).End(xlDown).Row, _
3))


minimumValue = Format(Application.Min(searchArea), "dd/mm/yyyy hh:mm:ss")


Range(Cells(i, 1), Cells(i, 3)).Copy
Range("D100").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats


End If


Next i


MsgBox personname & " " & minimumValue
End Sub


My data layout is as follows

[TABLE="width: 265"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Teller [/TD]
[TD]Branch[/TD]
[TD]Time[/TD]
[/TR]
[TR]
[TD]Peter[/TD]
[TD]Branch Name[/TD]
[TD="align: right"]12/5/2017 14:03[/TD]
[/TR]
[TR]
[TD]Peter[/TD]
[TD]Branch Name[/TD]
[TD="align: right"]12/5/2017 18:01[/TD]
[/TR]
[TR]
[TD]Peter[/TD]
[TD]Branch Name[/TD]
[TD="align: right"]12/5/2017 17:59[/TD]
[/TR]
[TR]
[TD]John[/TD]
[TD]Branch Name2[/TD]
[TD="align: right"]12/5/2017 12:03[/TD]
[/TR]
[TR]
[TD]John[/TD]
[TD]Branch Name2[/TD]
[TD="align: right"]12/5/2017 22:01[/TD]
[/TR]
[TR]
[TD]John[/TD]
[TD]Branch Name2[/TD]
[TD="align: right"]12/5/2017 17:59[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,226,113
Messages
6,189,046
Members
453,522
Latest member
Seeker2025

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top