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

Teller BranchTime
PeterBranch Name12/5/2017 14:03
PeterBranch Name12/5/2017 18:01
PeterBranch Name12/5/2017 17:59
JohnBranch Name212/5/2017 12:03
JohnBranch Name212/5/2017 22:01
JohnBranch Name212/5/2017 17:59

<colgroup><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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