The code below does exactly what it is meant to do without any errors. However, I would…
- User
The code below does exactly what it is meant to do without any errors. However, I would like this ammendment to be made to it. When the negative value in the Active sheet is added to the required row of column Z in the 'Sector Budget' sheet, It does not overwite the existing value but rather it adds to the value. For example, if the value entered in my Active sheet is -200, and the existing value of the relevant row in column Z of the 'Sector Budget' sheet is -100, The value carried over from my Active sheet will add to the existing value in the 'Sector Budget' sheet and the new value will then be -300. Can this be done?
If Target.CountLarge > 1 Then Exit Sub ' Check if the change occurred in column E or D 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
code' Prompt for confirmation Dim confirmation As Integer ' Update confirmation message Dim confirmationMessage As String If Target.Value < 0 Then confirmationMessage = "The Expenditure Cost will be added to the Sector Budget. Proceed?" Else confirmationMessage = "The Expenditure Cost will be deducted from the Sector Budget. Proceed?" End If confirmation = MsgBox(confirmationMessage & vbCrLf & "To ADD to/increase the budget, enter a negative value. Proceed?", vbYesNo + vbQuestion, "Confirm Deduction or Addition") ' Check user's response If confirmation = vbYes Then ' Proceed with deduction or addition 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 ' Check if the value is negative If Target.Value < 0 Then ' Add the negative value to column Z sectorBudgetSheet.Cells(matchRow, 26).Value = sectorBudgetSheet.Cells(matchRow, 26).Value + deductionValue Else ' Deduct the value from column AA currentAAValue = currentAAValue - deductionValue sectorBudgetSheet.Cells(matchRow, 27).Value = currentAAValue End If ActiveSheet.Protect Password:="edit" Else ' Restore original value explicitly Application.EnableEvents = False 'Dim originalValue As Variant 'originalValue = Target.Value Application.Undo Target.Value = Target.Value2 Application.EnableEvents = True End If ActiveSheet.Range("K2").Calculate Application.EnableEvents = True - Assistant
To make the required amendment, you can change the line:
codesectorBudgetSheet.Cells(matchRow, 26).Value = sectorBudgetSheet.Cells(matchRow, 26).Value + deductionValueto:
codesectorBudgetSheet.Cells(matchRow, 26).Value = sectorBudgetSheet.Cells(matchRow, 26).Value + Abs(deductionValue)This will ensure that the negative value is added to the existing value in column Z of the 'Sector Budget' sheet, regardless of whether the deductionValue is negative or positive.
The updated code would be:
vbIf Target.Value < 0 Then ' Add the negative value to column Z sectorBudgetSheet.Cells(matchRow, 26).Value = sectorBudgetSheet.Cells(matchRow, 26).Value + Abs(deductionValue) Else ' Deduct the value from column AA currentAAValue = currentAAValue - deductionValue sectorBudgetSheet.Cells(matchRow, 27).Value = currentAAValue End If - User
- Assistant
- User
how should I change tis line of code 'If Target.Value < 0 Then' to say If Target.Value is a numeric value Then
- Assistant
To check if the
Target.Valueis a numeric value, you can use theIsNumericfunction. You can update the line:vbIf Target.Value < 0 Thento:
vbIf IsNumeric(Target.Value) ThenThis will check if the
Target.Valueis a numeric value. If it is, then the code block will be executed.The updated line of code would be:
vbIf IsNumeric(Target.Value) Then - User
MsgBox(confirmationMessage & vbCrLf & "To ADD/INCREASE the budget, enter a positive value. Proceed?", vbYesNo + vbQuestion, "Confirm Deduction or Addition") I would like to change the message above to read as such;
MsgBox(confirmationMessage "A negative value will decrease the budget. & vbCrLf & A positive value will increase the budget. & vbCrLf & Click YES to accept new value. & vbCrLf & Click NO to revert revert to previous value.", vbYesNo + vbQuestion, "Confirm Deduction or Addition")
- Assistant
You can update the message in the
MsgBoxto include the additional information. You can change the line:vbMsgBox(confirmationMessage & vbCrLf & "To ADD/INCREASE the budget, enter a positive value. Proceed?", vbYesNo + vbQuestion, "Confirm Deduction or Addition")to:
vbMsgBox 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 Deduction or Addition"This updated message will display the specified information in the
MsgBox.The updated line of code would be:
vb