Using VBA in excel to provide a warning on start up if a value in a column falls below an integer -
i have excel document multiple worksheets. 1 of worksheets titled "inventory". tracks how of each product have , when these products expire. in 1 of columns, lists "days until expiration" each material have. want write vba script every time open excel file, script runs , checks values in "days until expiration column". if there value in column within 14 days of expiration, want script pop-up upon excel file starting , "____ material has # days left before expiration". please advise.
assuming expiration dates on column d, please consider following code added workbook_open
event:
private sub workbook_open() dim wb workbook dim ws worksheet dim rngused range, rngexpirationcolumn range, rngcell range dim strexpirationmessage string set wb = thisworkbook set ws = wb.sheets("inventory") set rngused = ws.usedrange set rngexpirationcolumn = intersect(ws.columns(4), rngused) each rngcell in rngexpirationcolumn.cells if date - cdate(rngcell.value2) >= 14 if len(strexpirationmessage) = 0 strexpirationmessage = rngcell.offset(0, -3).value2 & " material has " & (date - cdate(rngcell.value2)) & " days left before expiration" else strexpirationmessage = strexpirationmessage & chr(13) & rngcell.offset(0, -3).value2 & " material has " & (date - cdate(rngcell.value2)) & " days left before expiration" end if end if next msgbox strexpirationmessage end sub
Comments
Post a Comment