VBA to modify cell formula

default_name

Board Regular
Joined
May 16, 2018
Messages
170
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hey guys,

I am trying to get some inputted cell values to change, based on values in another column.
Explanation:
There are two columns of values, a Bucket and Actual.
The bucket value represents an amount that is 'in the bucket'. Each bucket value can vary or change depending on the user input.
As the Actual value increases, the Bucket value decreases.
This can be achieved by simply using a subtraction formula in the Bucket cell.
(see below for a visual)

AB
1BucketActual
240 (=40-B2)
315 (=30-B3)15
450 (=100-B4)50

However, I am looking to make this workbook a little bit more intuitive so that the user does NOT need to set up the Bucket formula every time.
Is there a way (possibly using VBA?) to automatically have the Bucket values (in column A) change depending on the Actual values (in column B)?

Basically, it would be nice to just be able to type in the value of 40 into A2 and have it decrease by whatever is placed in B2 (without having to enter in the =40-B2 formula into A2)
Is something like this even possible?

Thanks in advance
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You can put this code in the worksheet change event:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Intersect(Target, Range(Cells(1, 1), Cells(10, 1))) Is Nothing) Then
 rowno = Target.Row
 Application.EnableEvents = False
  Cells(rowno, 1) = Cells(rowno, 1) - Cells(rowno, 2)
 Application.EnableEvents = True
End If

End Sub
Note this doesn't change the value in column A if the value in column B changes, but you didn't ask for that
 
Upvote 0
An alternative is to use this code to write the equation in the cell, which takes the value which has been entered:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Intersect(Target, Range(Cells(1, 1), Cells(10, 1))) Is Nothing) Then
rowno = Target.Row
Valu = Target.Value

Application.EnableEvents = False
  Cells(rowno, 1) = "=" & Valu & "-B" & rowno
Application.EnableEvents = True
End If

End Sub
This one will change if column B changes
 
Upvote 0
Solution
An alternative is to use this code to write the equation in the cell, which takes the value which has been entered:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Intersect(Target, Range(Cells(1, 1), Cells(10, 1))) Is Nothing) Then
rowno = Target.Row
Valu = Target.Value

Application.EnableEvents = False
  Cells(rowno, 1) = "=" & Valu & "-B" & rowno
Application.EnableEvents = True
End If

End Sub
This one will change if column B changes
This one works perfectly!
Thank you!!
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,684
Members
448,977
Latest member
dbonilla0331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top