xxdylantxx
New Member
- Joined
- Mar 16, 2021
- Messages
- 3
- Office Version
- 2013
- Platform
- Windows
I have this script that changes the value of one cell by whatever increment or detriment is done to a second cell. IE if I have 32 and expend 1 the first cell goes to 31. If I expend 2 more the first cell goes to 29. I am trying to figure out how I can expand this script to make it work for 2 more cells that are independant of these 2 cells but work the same way. I have tried to add more cells as ranges and put them into the script but it hasn't worked any way I have tried it. Any ideas?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim firstCell As Range, secondCell As Range, nV#, oV#
Set firstCell = [A1]
Set secondCell = [A2]
Application.EnableEvents = False
If Not Intersect(Target, firstCell) Is Nothing Then
If Target.Cells.Count > 1 Then
MsgBox firstCell.Address(0, 0) & " cannot be. changed at the same time. "
Application.Undo
GoTo e
End If
nV = firstCell
Application.Undo
oV = firstCell
firstCell = nV
If nV > oV Then secondCell = secondCell - nV + oV
End If
e: Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim firstCell As Range, secondCell As Range, nV#, oV#
Set firstCell = [A1]
Set secondCell = [A2]
Application.EnableEvents = False
If Not Intersect(Target, firstCell) Is Nothing Then
If Target.Cells.Count > 1 Then
MsgBox firstCell.Address(0, 0) & " cannot be. changed at the same time. "
Application.Undo
GoTo e
End If
nV = firstCell
Application.Undo
oV = firstCell
firstCell = nV
If nV > oV Then secondCell = secondCell - nV + oV
End If
e: Application.EnableEvents = True
End Sub