Hello Mr.Excel forum -
I have a function in my workbook that when selected, the data on the worksheet saves to a connected database. All of that functionality works well. I am now trying to implement some sort of check that will prevent the code from saving the data if a condition is not met. In the below code, I am trying to get the code to check if a "Request Status" field in column P is not "Not Started" then check to see if it has a "Last Modified Date" filled in in Column O. If there is not a date filled in and Column P is not "Not Started" then I would like the sub to exit and a message box to display. I have been testing the code below and know that it is working to check these conditions but I cannot figure out how to get it to exit if the condition is met. If I add "Exit Sub" to the end of the if statement the entire module always exists and even if the condition is not met, it still exits. Any ideas?
Thanks.
I have a function in my workbook that when selected, the data on the worksheet saves to a connected database. All of that functionality works well. I am now trying to implement some sort of check that will prevent the code from saving the data if a condition is not met. In the below code, I am trying to get the code to check if a "Request Status" field in column P is not "Not Started" then check to see if it has a "Last Modified Date" filled in in Column O. If there is not a date filled in and Column P is not "Not Started" then I would like the sub to exit and a message box to display. I have been testing the code below and know that it is working to check these conditions but I cannot figure out how to get it to exit if the condition is met. If I add "Exit Sub" to the end of the if statement the entire module always exists and even if the condition is not met, it still exits. Any ideas?
Thanks.
Code:
Dim lastrow As Long
lastrow = ws.Range("B" & Rows.Count).End(xlUp).Row
With ws
For i = 2 To lastrow
If Len(Trim(.Range("A" & i).Value)) <> 0 Then _
If Range("P" & i).Value <> "Not Started" Then _
If Len(Trim(.Range("O" & i).Value)) = 0 Then _
MsgBox "The save has been cancelled. Please ensure all requests labeled as ""In Progress"" or ""Completed"" have a valid Last Modified Date filled in.", vbCritical, "Error"
Next i
End With