VBA to allow user to change either of two cells and have formula automatically update in both

J00490

New Member
Joined
May 17, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet for pricing items where I have a formula to calculate the total discount in one cell and total price in another. in this state they don't reference each other, just provide the answer.
I want to allow users to overwrite either of these cells and have the formula change to reflect this.
e.g.
Let's assume that the total price was £100 and the total discount 0% at the start.
1. I want to allow users to overwrite the calculated discount cell with, say 10%, this then changes the total price cell to reflect a 10% discount (£90),
2. Alternatively, they could change the total price cell to £90 and the discount cell would update to reflect the 10% change.
If either of the cells are then cleared/deleted, I want the original formulas to come back and show the £100, 0% that was originally calculated.

I know it's possible with VBA, I just don't know how!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
@J00490

Maybe something like the below?
Using the test data:
MRXLMAY21.xlsm
ABCDEFGH
1Total PriceTotal DiscPrc1Prc2Prc3Disc1Disc2
210010.00%305020010
3
Sheet1
Cell Formulas
RangeFormula
A2A2=D2+E2+F2
B2B2=(G2+H2)/100


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub

Dim TotPrice As Range
Dim TotDisc As Range
Dim Ex As String

Set TotPrice = Range("A2")  '<<Edit ranges to suit
Set TotDisc = Range("B2")   '<<Edit ranges to suit

If Not Intersect(Target, TotPrice) Is Nothing Then Ex = "Price"
If Not Intersect(Target, TotDisc) Is Nothing Then Ex = "Disc"
If Ex = vbNullString Then Exit Sub

Application.EnableEvents = False

PriceForm = "=D2+E2+F2"   '<<Edit formula to suit
DiscForm = "=(G2+H2)/100"   '<<Edit formula to suit


If Not Left(Target.Formula, 1) = "=" Then   'Formula has been overwritten

    If Target.Value = vbNullString Then  'reset original formulas
        TotPrice.Formula = PriceForm
        TotDisc.Formula = DiscForm
        Application.EnableEvents = True
        Exit Sub
    End If
    
    
    
    Select Case Ex
        Case "Price"
        'Use below formula if initial discount is always 0%
           ' TotDisc = 1 - (TotPrice / (Evaluate(PriceForm)))
        'Otherwise this below??????
            TotDisc = 1 - (TotPrice / (Evaluate(PriceForm))) + Evaluate(DiscForm)
        
        Case "Disc"
         'Use below formula if initial discount is always 0%
            TotPrice = (1 - TotDisc) * Evaluate(PriceForm)
           'Otherwise this below??????
            'TotPrice = (1 - (TotDisc - Evaluate(DiscForm))) * Evaluate(PriceForm)
            
    End Select



End If
Application.EnableEvents = True
End Sub

Give that a test. I'm not sure which of the alternative expressions you would want to use. Maybe the second if your relationship is taking into account the initial discount?

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,812
Members
449,048
Latest member
greyangel23

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