With this code I want the message to run if any of the cells from a to f are empt.…
- User
With this code I want the message to run if any of the cells from a to f are empt. Private Sub Worksheet_Activate() If Not IsEmpty(ThisWorkbook.Sheets("Job Request").Range("B2")) Then 'check if B2 is not blank Dim answer As VbMsgBoxResult answer = MsgBox("Do you want to delete the Request Entry", vbYesNo + vbQuestion, "Delete contents?") If answer = vbYes Then ThisWorkbook.Sheets("Job Request").Range("A2:F2").ClearContents End If End If End Sub
- Assistant
Here's an updated code that will check if any of the cells from A2 to F2 are empty before showing the message:
Private Sub Worksheet_Activate() Dim rng As Range Set rng = ThisWorkbook.Sheets("Job Request").Range("A2:F2") 'set the range to check
If Application.WorksheetFunction.CountBlank(rng) > 0 Then 'check if any cell in the range is blank Dim answer As VbMsgBoxResult answer = MsgBox("Some cells are empty. Do you want to delete the Request Entry?", vbYesNo + vbQuestion, "Delete contents?") If answer = vbYes Then rng.ClearContents 'clear the contents of the range End If End IfEnd Sub
This code uses the CountBlank function to count the number of blank cells in the range. If the count is greater than 0, then the message will be shown. If the user chooses to delete the contents, the code will clear the contents of the entire range.
- User
If A2 to F2 are all blank the it should exit the task
- Assistant
Here's the updated code that will exit the task if all cells from A2 to F2 are empty:
Private Sub Worksheet_Activate() Dim rng As Range Set rng = ThisWorkbook.Sheets("Job Request").Range("A2:F2")
If Application.WorksheetFunction.CountBlank(rng) = rng.Cells.Count Then 'check if all cells in the range are blank Exit Sub 'exit the task if all cells are blank End If If Application.WorksheetFunction.CountBlank(rng) > 0 Then Dim answer As VbMsgBoxResult answer = MsgBox("Some cells are empty. Do you want to delete the Request Entry?", vbYesNo + vbQuestion, "Delete contents?") If answer = vbYes Then rng.ClearContents End If End IfEnd Sub
This code adds another check using the same CountBlank function. If the count of blank cells in the range is equal to the total number of cells in the range, it means all cells are blank and the code will exit the task. Otherwise, it will execute the same message and clear contents prompt as before.
- User
Please correct =IF(OR(G3,H3,)="","PERMIT REQUIRED","")
- Assistant
Assuming you want to check if either cell G3 or H3 is blank, here's the corrected formula:
=IF(OR(G3="",H3=""),"PERMIT REQUIRED","")
This formula will return "PERMIT REQUIRED" if either G3 or H3 is blank, and an empty string otherwise.