Tanyaann1995
Board Regular
- Joined
- Mar 24, 2021
- Messages
- 62
- Office Version
- 2016
- Platform
- Windows
Hi,
I'm using the below code to filter out a series of data. First, the customer name has to be filtered, then the Item has to be filtered and then the most recent date in Column CA has to be found. I want the code to find the most recent date from the filtered entries in that column but the result shows the most recent date in the entire column. Please can you advise how to fix this. I have attached sample of how the sheet looks like.
Dim i As Integer
Dim lRow As Long
Dim pno As String
Dim name As String
Dim max_date As Date
Dim pfind As Range
Dim m As Long
Dim n As String
lRow = ThisWorkbook.Worksheets(2).Range("E:E").Find(what:="*", _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
name = InputBox("Input customer name to search against")
For i = 24 To lRow
If IsEmpty(Cells(i, 4).Value) = False Then
pno = ThisWorkbook.Worksheets(2).Cells(i, 4).Value
With Workbooks("Latest hist prices 3.xlsx").Worksheets(1)
.Range("A1:DV25290").AutoFilter Field:=67, Criteria1:="=*" & pno & "*"
.Range("A1:DV25290").AutoFilter Field:=15, Criteria1:="=*" & name & "*"
max_date = Application.WorksheetFunction.Max(.Columns("CA"))
Set pfind = .Range("CA:CA").Find(what:=max_date, LookIn:=xlValues, LookAt:=xlWhole)
m = pfind.Offset(0, -27).Value
n = pfind.Offset(0, -28).Value
ThisWorkbook.Worksheets(2).Cells(i, 21).Value = m & n & "-" & max_date
End With
End If
Next i
End Sub
I'm using the below code to filter out a series of data. First, the customer name has to be filtered, then the Item has to be filtered and then the most recent date in Column CA has to be found. I want the code to find the most recent date from the filtered entries in that column but the result shows the most recent date in the entire column. Please can you advise how to fix this. I have attached sample of how the sheet looks like.
Dim i As Integer
Dim lRow As Long
Dim pno As String
Dim name As String
Dim max_date As Date
Dim pfind As Range
Dim m As Long
Dim n As String
lRow = ThisWorkbook.Worksheets(2).Range("E:E").Find(what:="*", _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
name = InputBox("Input customer name to search against")
For i = 24 To lRow
If IsEmpty(Cells(i, 4).Value) = False Then
pno = ThisWorkbook.Worksheets(2).Cells(i, 4).Value
With Workbooks("Latest hist prices 3.xlsx").Worksheets(1)
.Range("A1:DV25290").AutoFilter Field:=67, Criteria1:="=*" & pno & "*"
.Range("A1:DV25290").AutoFilter Field:=15, Criteria1:="=*" & name & "*"
max_date = Application.WorksheetFunction.Max(.Columns("CA"))
Set pfind = .Range("CA:CA").Find(what:=max_date, LookIn:=xlValues, LookAt:=xlWhole)
m = pfind.Offset(0, -27).Value
n = pfind.Offset(0, -28).Value
ThisWorkbook.Worksheets(2).Cells(i, 21).Value = m & n & "-" & max_date
End With
End If
Next i
End Sub