VBA Expiry date with entire row Color highlight & new column with status

mohanprabhus

New Member
Joined
Dec 7, 2016
Messages
17
I'm trying to write a VBA CODE example mentioned below. Help on the below-mentioned example


ABCD
SL NOPRODUCTMANUFACTURING DATEEXPIRY DATE
1ABC01-01-202101-01-2022
2XYZ29-03-202129-03-2022
3CDA04-05-201904-05-2020
4TWC23-03-202123-03-2021

I want my output as mentioned below


ABCDEF
SL NOPRODUCTMANUFACTURING DATEEXPIRY DATECURRENT DATESTATUS
1ABC01-01-202101-01-202223-03-2022OVERDUE in 81 Days
2XYZ29-03-202129-03-202223-03-2022Going to Expire in Next 6 Days
3CDA04-05-201904-05-202023-03-2022OVERDUE in 688 Days
4TWC23-03-202123-03-202123-03-2022Expiring today
5JSJD20-03-202120-03-202223-03-2022Expired in 3 days
6DDFDFA24-03-202124-03-202223-03-2022Expirining Tomorrow

Conditions
Overdue means more than 15 days from Current Date
Expired Today Current Date and Expiry Date
Expiring Tomorrow Current Date and Expiry Date
Going to Expiry Current date within 15 days

Advance in Thanks.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
the below may do what you want.
I have made an assumption that your DATA in cell D4 is incorrect as you have shown the result to be Expiring today but there is actually a year difference between the dates so should be expired 365 based on your conditions of current date - expiry date

VBA Code:
Dim ws As Worksheet
Dim Myday As Long
Dim cel As Range, MyRng As Range

Set ws = ActiveSheet
Set MyRng = ws.Range("D2:D" & ws.Cells(Rows.count, 4).End(xlUp).row)

For Each cel In MyRng
   ws.Cells(cel.row, 5) = Date
   Myday = ws.Cells(cel.row, 5) - ws.Cells(cel.row, 4)
   Select Case Myday
      Case 0: ws.Cells(cel.row, 6) = "Expiring Today"
      Case 1: ws.Cells(cel.row, 6) = "Expiring Tomorrow"
      Case 2 To 15: ws.Cells(cel.row, 6) = "Expires in " & Myday & " Days"
      Case Else: ws.Cells(cel.row, 6) = "Overdue in " & Myday & " Days"
   End Select
Next cel
 
Upvote 0
the below may do what you want.
I have made an assumption that your DATA in cell D4 is incorrect as you have shown the result to be Expiring today but there is actually a year difference between the dates so should be expired 365 based on your conditions of current date - expiry date

VBA Code:
Dim ws As Worksheet
Dim Myday As Long
Dim cel As Range, MyRng As Range

Set ws = ActiveSheet
Set MyRng = ws.Range("D2:D" & ws.Cells(Rows.count, 4).End(xlUp).row)

For Each cel In MyRng
   ws.Cells(cel.row, 5) = Date
   Myday = ws.Cells(cel.row, 5) - ws.Cells(cel.row, 4)
   Select Case Myday
      Case 0: ws.Cells(cel.row, 6) = "Expiring Today"
      Case 1: ws.Cells(cel.row, 6) = "Expiring Tomorrow"
      Case 2 To 15: ws.Cells(cel.row, 6) = "Expires in " & Myday & " Days"
      Case Else: ws.Cells(cel.row, 6) = "Overdue in " & Myday & " Days"
   End Select
Next cel
Thank you gordsky, But Expiring Tomorrow Does not work. And also request you to help the complete row to highlight with RED for OverDue, Expires within 15 days with Grey Color, Expiring Today with Blue Color & Expiring tomorrow, or within the next 15 days with green color.

Advance in Thanks.
 
Upvote 0
Thank you gordsky, But Expiring Tomorrow Does not work. And also request you to help the complete row to highlight with RED for OverDue, Expires within 15 days with Grey Color, Expiring Today with Blue Color & Expiring tomorrow, or within the next 15 days with green color.

Advance in Thanks.
you should really be more specific than "Doesnt work" however i think if you change the

VBA Code:
 Myday = ws.Cells(cel.row, 5) - ws.Cells(cel.row, 4)
to
VBA Code:
Myday = Abs(ws.Cells(cel.row, 5) - ws.Cells(cel.row, 4))
then it will work.

As you made no request for colours in your origional post one was not included. You would prob be better doing that through conditional formatting on the sheet
 
Upvote 0
you should really be more specific than "Doesnt work" however i think if you change the

VBA Code:
 Myday = ws.Cells(cel.row, 5) - ws.Cells(cel.row, 4)
to
VBA Code:
Myday = Abs(ws.Cells(cel.row, 5) - ws.Cells(cel.row, 4))
then it will work.

As you made no request for colours in your origional post one was not included. You would prob be better doing that through conditional formatting on the sheet
Thank you gordsky, It was much helpful & definitely i will do conditional formatting, But I do face one more problem here For example my Expiry is not past may be future dates like or 23-06-2022, 23-05-2023 then the output says overdue, actual it is not yet due. The message should Valid instead of overdue.

Advance in Thanks.
 
Upvote 0
Thank you gordsky, It was much helpful & definitely i will do conditional formatting, But I do face one more problem here For example my Expiry is not past may be future dates like or 23-06-2022, 23-05-2023 then the output says overdue, actual it is not yet due. The message should Valid instead of overdue.

Advance in Thanks.
It would be more helpful if you supplied all the info in one go. Again your origional post did not mention future dates only the 4 criteria the code was written for.

new code is

VBA Code:
Sub test()
Dim ws As Worksheet
Dim Myday As Long
Dim cel As Range, MyRng As Range

Set ws = ActiveSheet
Set MyRng = ws.Range("D2:D" & ws.Cells(Rows.count, 4).End(xlUp).row)

For Each cel In MyRng
   ws.Cells(cel.row, 5) = Date
   
   Myday = ws.Cells(cel.row, 4) - ws.Cells(cel.row, 5)
   Select Case Myday
      Case 0: ws.Cells(cel.row, 6) = "Expiring Today"
      Case 1: ws.Cells(cel.row, 6) = "Expiring Tomorrow"
      Case 2 To 15: ws.Cells(cel.row, 6) = "Expires in " & Myday & " Days"
      Case Is > 15: ws.Cells(cel.row, 6) = "Valid"
      Case Else: ws.Cells(cel.row, 6) = "Overdue by " & Myday * -1 & " Days"
   End Select
Next cel

End Sub
 
Upvote 0
It would be more helpful if you supplied all the info in one go. Again your origional post did not mention future dates only the 4 criteria the code was written for.

new code is

VBA Code:
Sub test()
Dim ws As Worksheet
Dim Myday As Long
Dim cel As Range, MyRng As Range

Set ws = ActiveSheet
Set MyRng = ws.Range("D2:D" & ws.Cells(Rows.count, 4).End(xlUp).row)

For Each cel In MyRng
   ws.Cells(cel.row, 5) = Date
   
   Myday = ws.Cells(cel.row, 4) - ws.Cells(cel.row, 5)
   Select Case Myday
      Case 0: ws.Cells(cel.row, 6) = "Expiring Today"
      Case 1: ws.Cells(cel.row, 6) = "Expiring Tomorrow"
      Case 2 To 15: ws.Cells(cel.row, 6) = "Expires in " & Myday & " Days"
      Case Is > 15: ws.Cells(cel.row, 6) = "Valid"
      Case Else: ws.Cells(cel.row, 6) = "Overdue by " & Myday * -1 & " Days"
   End Select
Next cel

End Sub
Did this solve your query
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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