The difference between numbers within same cell

Vxuser

New Member
Joined
Jun 29, 2019
Messages
9
Good day,

I am interestes in a formula or VBA code that can make the minus (-) between numbers in same cell.
In order to be more specific I will explain exactly what I want.

In cell A1 I will put a value (total no. of miles) at the begining, lets say 3000, than every day the no. of miles will decrease by a different values.
I want in the 1st day to type 2766 in cell A1 and to appear in cell B1 the diferrence 3000-2766= 234.

2nd day I will type 2501 in cell A1 and in B1 to appear the difference from the miles I did from day 1 to day 2 ... 2766-2501=265

3rd day I will type 2389 in cell A1 and in B1 to appear the difference from the miles i did from day 2 to day 3 .... 2501-2389=112

And so on.

At the moment I am using a VBA code In order to add the numbers but I need also one VBA code to substract the numbers. I will paste down the code for adding the numbers:

Private Sub Worksheet_Change (ByVal Target as Range)
If Not Application.Intersect(Range("A1"), Range(Target.Address)) Is Nothing Then
If Target.HasFormula = True Then Exit Sub
If Target.Value <> " " Then
Target.Offset(0,10).Value = Target.Offset(0,10).Value & " " & Target.Value
End If
End If
End Sub

And Module 1:

Function SumNums(pWorkRng As Range, Optional xDelim As String = " ") As Double
Dim arr As Variant
Dim xIndex As Long
arr = Split(pWorkRng, xDelim)
For xIndex = LBound(arr) To UBound(arr) Step 1
SumNums = SumNums + VBA.Val(arr(xIndex))
Next
End Function


Thank you for taking the time to read my post.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Going by your verbiage only, try this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldval As Double, newval As Double
If Target.Count > 1 Then Exit Sub
If Target.Address = "$A$1" Then
    Application.EnableEvents = False
    newval = Target.Value
    Application.Undo
    oldval = Target.Value
    Target = newval
    Target.Offset(1) = oldval - newval
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Thank you for your interest of helping me.

I have tried what you suggested but nothing is happening, not even the values to copy themselfs in the offset.
 
Upvote 0
Sorry for the previous reply.
I relooked on the code and I saw a mistake that I've made.

Your code is working.
Thank you so much!
 
Upvote 0
Placing a value in a cell and then trying to compute on it and replace it will create a circular reference.

Can't you use this?

ABC
730002766234
82501265
92389112

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet13

Worksheet Formulas
CellFormula
C7=A7-B7

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>


with C8 filled down: =B7-B8
 
Last edited:
Upvote 0
Good day,

I am interestes in a formula or VBA code that can make the minus (-) between numbers in same cell.
In order to be more specific I will explain exactly what I want.

In cell A1 I will put a value (total no. of miles) at the begining, lets say 3000, than every day the no. of miles will decrease by a different values.
I want in the 1st day to type 2766 in cell A1 and to appear in cell B1 the diferrence 3000-2766= 234.

2nd day I will type 2501 in cell A1 and in B1 to appear the difference from the miles I did from day 1 to day 2 ... 2766-2501=265

3rd day I will type 2389 in cell A1 and in B1 to appear the difference from the miles i did from day 2 to day 3 .... 2501-2389=112

And so on.

At the moment I am using a VBA code In order to add the numbers but I need also one VBA code to substract the numbers. I will paste down the code for adding the numbers:

Private Sub Worksheet_Change (ByVal Target as Range)
If Not Application.Intersect(Range("A1"), Range(Target.Address)) Is Nothing Then
If Target.HasFormula = True Then Exit Sub
If Target.Value <> " " Then
Target.Offset(0,10).Value = Target.Offset(0,10).Value & " " & Target.Value
End If
End If
End Sub

And Module 1:

Function SumNums(pWorkRng As Range, Optional xDelim As String = " ") As Double
Dim arr As Variant
Dim xIndex As Long
arr = Split(pWorkRng, xDelim)
For xIndex = LBound(arr) To UBound(arr) Step 1
SumNums = SumNums + VBA.Val(arr(xIndex))
Next
End Function


Thank you for taking the time to read my post.


Try this, It's another way to do it


Code:
Dim old
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address(0, 0) <> "A1" Or Target.Count > 1 Then Exit Sub
    old = Target.Value
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0, 0) <> "A1" Or Target.Count > 1 Then Exit Sub
    Target.Offset(1) = old - Target.Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,558
Members
449,038
Latest member
Guest1337

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