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