# vba for difference btw 2 numbers

1. ## vba for difference btw 2 numbers

i have an excel sheet1 with the following values

I want to write a macro to do the following:

(col A only has positive values, Col B has both positive and negetive values) I want to find the difference between the +ve and -ve values, keeping in mind the following;
if col A's value is less than col B's Values, (Eg: 1 -3) the result should be -2
if col A's value is equal than col B's Values, (Eg: 1 -1) the result should be 0
if col A's value is less than col B's Values, (Eg: 1 3) the result should be 2

QtyA QtyB
1 -1
1 -2
2 -2
3 1
1 -5
1 -3
5 2
1 1
2 3
5 -2
1 1
1 -1
6 -5
7 9
8 3

I want Col D to have the difference between the ColA And Col B.

The result should be this:

QtyA QtyB Result
1 -1 0
1 -2 -1
2 -2 0
3 1 2
1 -5 -4
1 -3 -2
5 2 3
1 1 0
2 3 1
5 -2 3
1 1 0
1 -1 0
6 -5 1
7 9 2
8 3 5  Register To Reply

2. ## Re: vba for difference btw 2 numbers

No VBA needed, you can use the formula below ``Please Login or Register  to view this content.``  Register To Reply

3. ## Re: vba for difference btw 2 numbers

Another way (without IF):

Formula:  `Please Login or Register  to view this content.`  Register To Reply

4. ## Re: vba for difference btw 2 numbers

im sorry i forgot to mention.....if both the numbers are equal i want to print "same"or "noChange".
eg. 2 2 same
1 1 same

How do i do that along with the above mentioned calculation???  Register To Reply

5. ## Re: vba for difference btw 2 numbers

ok i found it. i just tweaked your formula like this

=IF(\$B1=\$D1,"same",IF(\$B1<\$D1,\$D1-\$B1,\$B1-ABS(\$D1)))

and its working. Thanks a lot . MY problem is solved  Register To Reply