this routine does not detect the there are values in D2..... Private Sub…
- User
this routine does not detect the there are values in D2..... Private Sub Worksheet_Activate() Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True 'If Not IsEmpty(ThisWorkbook.Sheets("Job Request").Range("B2")) Then 'check if B2 is not blank Dim rng As Range Set rng = ThisWorkbook.Sheets("Job Request").Range("A2:F2") 'set the range to check
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 'check if any cell in the range is 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 Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True End If End If End Sub
- Assistant
To detect if there are values in D2, you can modify the code to check only that cell:
Private Sub Worksheet_Activate() Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True Dim rng As Range Set rng = ThisWorkbook.Sheets("Job Request").Range("D2") 'set the range to check If Not IsEmpty(rng) Then 'check if D2 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 Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True End If End If End SubThis will only prompt to delete the contents if D2 is not blank.
- User
I still want to check the entire range A2 to F2 for values
- Assistant
Understood. You can modify the code like this to check the entire range A2:F2 for values:
Private Sub Worksheet_Activate() Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True Dim rng As Range Set rng = ThisWorkbook.Sheets("Job Request").Range("A2:F2") 'set the range to check 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 'check if any cell in the range is 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 Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True End If End If End SubThis modified code will:
- Calculate the sheet when it is activated
- User
I want to routine to check if there are any values in the range A2:F2. If any value is found then it should ask me if I want to delete the values. However if there are no values in the range A2:F2, it should skip to the next routine
- Assistant
Here's the modified code to check if there are any values in the range A2:F2 and ask to delete the values:
Private Sub Worksheet_Activate() Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True Dim myRange As Range Set myRange = ThisWorkbook.Sheets("Job Request").Range("A2:F2") If WorksheetFunction.CountA(myRange) > 0 Then Dim message As String message = "Do you want to delete the Request Entry?" Dim response As VbMsgBoxResult response = MsgBox(message, vbYesNo + vbQuestion, "Delete contents?") If response = vbYes Then ThisWorkbook.Sheets("Job Request").Range("A2:F2").ClearContents Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True End If Else 'Skip to the next routine End If End SubThis code uses the
CountA()function to count the number of non-empty cells in the range A2:F2. If any non-empty cells are found, it will prompt the user to delete the values. If the user selects "yes", it will clear the contents of the range and recalculate the sheet.If there are no non-empty cells in the range, it will skip to the next routine.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.