I have an excel sheet (2003 and 2007) , in which I have got two columns FromDate and ToDate of the type DateTime in mm/dd/yyyy format.
There can be “n” number of records in the excel sheet.
I want to apply a validation , such that value of ToDate column should always be greater then equal to value of FromDate column, when the file is saved. If this is not the case then an error message should be prompted
EDIT: @brettdj made a great point – it may be the case that this already exists. If so, the following will show a message with each error (which will be super annoying if there are lots) and also
debug.printit. Note that my setup here is the same as in the screenshots below. This will need to go intoThisWorkbookin the VBA Editor, and the code dropped into theBeforeSaveevent (this simply shows the errors and then continues to the save prompt):If you are entering data…
Do you require it to use VBA? I only ask because you could do this without VBA using Data Validation:
1.) Set up your data, select the
ToDatecolumn (don’t include the column header) and clickData->Data Validationon the Ribbon (I’m using Excel 2007), change theAllowdrop-down toDate, change theDatafield togreater than or equal toand enter=A2for theStart Datefield.Now enter a few values into column
B, including ones that aren’t ‘correct’ (this includes any non-date [a date as defined by the cell format, in my casemm/dd/yyyy] as well as one that is before theToDate). You will receive an error message (which you can also adjust if needed). Happy to modify the answer if you do in fact need/prefer to use VBA.