Try this code
Option Explicit
Sub expiry_dates()
Dim i As Long, lrow As Long, a As Long, j As Long
Application.ScreenUpdating = False
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Report"
For i = 1 To Worksheets.Count
With Worksheets(i)
If .Name <> "Report" And .Name <> "Sheet1" Then
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
For a = 1 To lrow
For j = 2 To 9
If .Cells(a, j).Value - Date <= 14 And .Cells(a, j).Value <> "" Then
Worksheets("Report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = .Cells(a, 1).Value
Worksheets("Report").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = .Name
End If
Next j
Next a
End If
End With
Next i
Application.ScreenUpdating = True
End Sub
Copy the Excel VBA code
Select the workbook in which you want to store the Excel VBA code
Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
Choose Insert | Module
Where the cursor is flashing, choose Edit | Paste
To run the Excel VBA code:
Choose View | Macros
Select a macro in the list, and click the Run button
The output gives multiple rows for the same medicine if it finds more than 1 vial having the same date <= 14 days. Let me know if you need a code to remove the duplicate values.
Bookmarks