I do not want the following code to run on cell I2. Dim confirmation As Integer On Error…
- User
I do not want the following code to run on cell I2. Dim confirmation As Integer On Error Resume Next Application.EnableEvents = False confirmation = MsgBox(“Do you want to save this change?”, vbYesNo) If confirmation = vbNo Then Application.Undo End If Application.EnableEvents = True
- Assistant
As an AI language model, I am unable to control or restrict the execution of any code. However, you can add a condition to check if the active cell is not I2 before running the code.
For example, you can wrap the code in an If statement like this:
codeSub yourSubName() If ActiveCell.Address <> "$I$2" Then Dim confirmation As Integer On Error Resume Next Application.EnableEvents = False confirmation = MsgBox("Do you want to save this change?", vbYesNo) If confirmation = vbNo Then Application.Undo End If Application.EnableEvents = True End If End SubThis way, the code will only execute if the active cell is not I2.
- User
is there a neater way of writing this code. Private Sub Worksheet_Change(ByVal Target As Range) If activeCell.Address <> "2" Then Dim confirmation As Integer On Error Resume Next Application.EnableEvents = False confirmation = MsgBox("Do you want to save this change?", vbYesNo) If confirmation = vbNo Then Application.Undo End If Application.EnableEvents = True End If 'If Not Intersect(Target, Range("2")) Is Nothing Then If Target.Address = "2" Then Application.EnableEvents = True Application.CalculateFullRebuild 'MsgBox ("Close Prompt, Press Keyboard F9 or Scroll Up Down to show Results") End If End Sub
- Assistant
Yes, you can simplify and clean up the code with the following changes:
- Remove the
On Error Resume Nextstatement, as it can suppress potential errors and make debugging more difficult. - Combine the two
Application.EnableEventsstatements into one, placed at the end of the procedure. - Use early binding to declare the
confirmationvariable for better performance and easier code maintenance. - Replace the
If activeCell.Address <> "$I$2" Thenstatement with a simpleExit Substatement when the active cell is I2. This way, you can avoid an unnecessary nestedIfstatement.
Here's the updated code:
code - Remove the
- User
If the Target.Address is I2 I want to skip - confirmation = MsgBox(“Do you want to save this change?”, vbYesNo) If confirmation = vbNo Then Application.Undo, but continue to Application.EnableEvents = True Application.CalculateFullRebuild ’ MsgBox (“Close Prompt, Press Keyboard F9 or Scroll Up Down to show Results”)
- Assistant
Here's the updated code that skips the confirmation and undo steps if the
Targetcell is I2 but still enables events and recalculates formulas:codePrivate Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$I$2" Then ' Exit the sub if the target is I2 Application.EnableEvents = True Application.CalculateFullRebuild ' MsgBox ("Close Prompt, Press Keyboard F9 or Scroll Up Down to show Results") Exit Sub End If ' Prompt for confirmation before undoing changes Dim confirmation As VbMsgBoxResult confirmation = MsgBox("Do you want to save this change?", vbYesNo) If confirmation = vbNo Then Application.Undo End If ' Enable events and recalculate formulas Application.EnableEvents = True Application.CalculateFullRebuild ' MsgBox ("Close Prompt, Press Keyboard F9 or Scroll Up Down to show Results") End Sub - User
how can you create an anchor point within a vba code and jump to the anchor point within the code
- Assistant
In VBA, you can create a label as an anchor point in your code and use the
GoTostatement to jump to that label. Here's an example:codeSub TestAnchorPoint() ' Some code here AnchorPoint: ' Code to execute at the anchor point ' More code here End SubIn this example,
AnchorPoint:is the label that serves as an anchor point, and theGoTostatement can be used to jump to it, like this:codeSub TestAnchorPoint() ' Some code here GoTo AnchorPoint ' More code here AnchorPoint: ' Code to execute at the anchor point ' More code here End SubNote that using
GoTostatements can make your code harder to read and debug, so it's generally recommended to avoid them if possible. Instead, try to structure your code using subroutines and functions with clear names and well-defined inputs and outputs. - User
I am getting a loop when I click No on the last message - Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "2" Then ' Exit the rest of the sub if the target is I2 Application.EnableEvents = True Application.CalculateFullRebuild MsgBox ("Close Prompt, Press Keyboard F9 or Scroll Up Down to show Results") Exit Sub End If
' Prompt for confirmation before undoing changes Dim confirmation As VbMsgBoxResult confirmation = MsgBox("Do you want to save this change?", vbYesNo) If confirmation = vbNo Then Application.Undo Exit Sub End If
End Sub
- Assistant
The issue with your code is that it doesn't prevent further event triggers from happening after the
Undooperation is performed, which can lead to a loop.One way to fix this is to temporarily disable event handling (using
Application.EnableEvents = False) before performing theUndooperation, and then re-enable it afterwards (usingApplication.EnableEvents = True). This will prevent theWorksheet_Changeevent from being triggered again by theUndooperation. Here's an updated version of your code that should work:code