The event below works perfectly. When I enter a value in the range D6:D305 of my active…
- User
The event below works perfectly. When I enter a value in the range D6:D305 of my active sheet, it deducts the budget total in the required row of sheet "Sector Budget". I am wondering if it is possible ADD to this event to do the opposite such as, If I enter a negative value (for example -100) in the range D6:D305 of my active sheet, it adds the negative value to column Z in the required row of sheet "Sector Budget" The new code addition must must only be triggered if the Target numeric value is negative. If the Target numeric value is NOT negative, then the original part of the event should run. The Message also needs to change to read 'The Expenditure Cost will be either be deducted from or added to the Sector Budget. (LINE SPACE) to ADD to/increase the budget, enter a negative value. Proceed?'
If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Me.Range("D6:D305")) Is Nothing Then If Target.CountLarge = 1 And Target.Value <> "" Then Application.EnableEvents = False MsgBox "If there has been an Expenditure, please enter cost in the column E" Application.EnableEvents = True End If End If
' Deduct from Budget Balance If Target.CountLarge > 1 Then Exit Sub ' Check if the change occurred in column E If Not Intersect(Target, Me.Range("E6:E305")) Is Nothing Then ' Verify that the entered value is numeric If Not IsNumeric(Target.Value) Then Application.EnableEvents = False MsgBox "Please enter a numeric value." Application.EnableEvents = True Exit Sub End If ' Prompt for confirmation Dim confirmation As Integer confirmation = MsgBox("The Expenditure Cost will be deducted from the Sector Budget. Proceed?", vbYesNo + vbQuestion, "Confirm Deduction") ' Check user's response If confirmation = vbYes Then ' Proceed with deduction logic Dim deducedCell As Range Set deducedCell = Target.Cells(1) Application.EnableEvents = False 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 currentAAValue = currentAAValue - deductionValue sectorBudgetSheet.Cells(matchRow, 27).Value = currentAAValue ActiveSheet.Protect Password:="edit" Else ' Restore original value explicitly Application.EnableEvents = False Target.Value = Target.originalValue Application.EnableEvents = True End If ActiveSheet.Range("K2").Calculate Application.EnableEvents = True End If - Assistant
- User
In the ammendment you have kindly provided, if I enter a negative value in my Active Sheet, will the negative value be added to the correct row of column Z in the 'Sector Budget' sheet
- Assistant
- User
The code you provided works perfectly when I click 'Yes'. Unfortunately when I click 'No' this part of the code fails: Else ' Restore original value explicitly Application.EnableEvents = False Target.Value = Target.originalValue Application.EnableEvents = True End If ActiveSheet.Range("K2").Calculate Application.EnableEvents = True End If
- Assistant
- User
I am no longer getting an error, but when I enter a new value and then click NO, the original cell value is not restored
- Assistant