In the VBA event below, if I enter a value in the Target cell of my Active sheet, I get…
- User
In the VBA event below, if I enter a value in the Target cell of my Active sheet, I get the Yes No Message, but when I click Yes the value in the Target cell of my Active sheet will not change.
' Prompt for confirmation Dim confirmation As Integer ' Update confirmation message Dim confirmationMessage As String If IsNumeric(Target.Value) Then MsgBox confirmationMessage & vbCrLf & "A negative value will decrease the budget." & vbCrLf & "A positive value will increase the budget." & vbCrLf & "Click YES to accept the new value." & vbCrLf & "Click NO to revert to the previous value.", vbYesNo + vbQuestion, "Confirm Budget Change" ' Check user's response If confirmation = vbYes Then ' Proceed with deduction or addition logic Dim deducedCell As Range Set deducedCell = Target.Cells(1) Dim deductionValue As Double deductionValue = deducedCell.Value Dim sectorBudgetSheet As Worksheet Set sectorBudgetSheet = ActiveWorkbook.Worksheets("Sector Budget") Dim searchValue As String searchValue = ActiveSheet.Range("D1").Value Dim matchRow As Long matchRow = sectorBudgetSheet.Range("A:A").Find(searchValue, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False).Row 'If matchRow = 0 Then Exit Sub Dim currentAAValue As Double currentAAValue = sectorBudgetSheet.Cells(matchRow, 27).Value Application.EnableEvents = False
sectorBudgetSheet.Cells(matchRow, 26).Value = sectorBudgetSheet.Cells(matchRow, 26).Value + deductionValue 'sectorBudgetSheet.Cells(matchRow, 26).Value = sectorBudgetSheet.Cells(matchRow, 26).Value + Abs(deductionValue) sectorBudgetSheet.Range("B2:B22").Calculate Application.EnableEvents = True Else ' Restore original value explicitly Application.EnableEvents = False Application.Undo Target.Value = Target.Value2 Application.EnableEvents = True End If ActiveSheet.Range("K2").CalculateEnd If End If
- Assistant
The issue in the code is that the value of the "confirmation" variable is never set to the result of the MsgBox.
To fix this, you need to assign the result of the MsgBox to the "confirmation" variable:
confirmation = MsgBox(confirmationMessage & vbCrLf & "A negative value will decrease the budget." & vbCrLf & "A positive value will increase the budget." & vbCrLf & "Click YES to accept the new value." & vbCrLf & "Click NO to revert to the previous value.", vbYesNo + vbQuestion, "Confirm Budget Change")This will store the user's response in the "confirmation" variable and allow you to check it later in the code.
- User
Can you please correct this: confirmation = MsgBox(confirmationMessage & vbCrLf & "A negative value will decrease the budget. & vbCrLf & "" & vbCrLf & _ "A positive value will increase the budget." & vbCrLf & "" & vbCrLf & _ "Click YES to accept the new value." & vbCrLf & "" & vbCrLf & _ "Click NO to revert to the previous value.", vbYesNo + vbQuestion, "Confirm Budget Change")
- Assistant
Certainly! Here's the corrected version:
confirmation = MsgBox(confirmationMessage & vbCrLf & "A negative value will decrease the budget." & vbCrLf & vbCrLf & _ "A positive value will increase the budget." & vbCrLf & vbCrLf & _ "Click YES to accept the new value." & vbCrLf & vbCrLf & _ "Click NO to revert to the previous value.", vbYesNo + vbQuestion, "Confirm Budget Change")In this version, I have removed the unnecessary quotation marks and added extra line breaks using
vbCrLfto improve the readability of the message.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.