Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Is there a built in VB6 function the allows you to pass in 2 numbers (these numbers can be either negative or postive) and get back the difference which could be a negative or positive difference..??

Thanks Ian

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Otherwise, a simple subtraction as hongjun posted will work.

You can also get the absolute value of a number if necessary using abs(YourNumber).

I suppose I don't really understand what you are trying to do. Isn't that exactly a difference?

Like

A=10

B=-15

C=A-B=10-(-15)=10+15=25

Is this what you want?

diasf

Example 1: -30 and 50 = a positive increase of +20

Example 2: 50 and -60 = a negative decrease of -10

Example 3: 100 and 150 = a positive increase of +50

Example 4: -50 and -20 = a positive increase of +30

Example 5: -150 and -200 = a negative decrease of -50

Ian

Private Sub Form_Load()

Dim intExample As Integer

intExample = diff(-30, 50)

MsgBox intExample

End Sub

Public Function diff(a As Integer, b As Integer) As Integer

diff = a + b

End Function

Example 2: 50 and -60 = a negative decrease of -10 ' hsould this be -110

Example 3: 100 and 150 = a positive increase of +50

Example 4: -50 and -20 = a positive increase of +30

Example 5: -150 and -200 = a negative decrease of -50

if yes then

Private Sub command1_Click()

Debug.Print diff(-30, 50)

Debug.Print diff(50, -60)

Debug.Print diff(100, 150)

Debug.Print diff(-50, -20)

Debug.Print diff(-150, -200)

End Sub

Public Function diff(a As Integer, b As Integer) As Integer

diff = b - a

End Function

Example 2: 50 and -60 = a negative decrease of -10

Unless EDDYKT is right and you do mean an increase of +80 and a decrease of -110 are you trying to compare the absolute values and after that the change of sign?

Like this?

Public Function diff(a As Integer, b As Integer) As Integer

'Declare Vars

Dim intDiff As Integer

'

intDiff = Abs(b) - Abs(a)

If a + b > 0 Then

diff = intDiff

ElseIf a + b < 0 Then

diff = -intDiff

ElseIf a + b = 0 Then

diff = b - a

End If

End Function

This function gives your desired output

Good luck

Atan Asfaloth

-150 + -50 then i get -200

That is the correct answer

If you are looking to work out difference, then you always need to subtract ( -150) - (-50) = -100

Mike

Yes this is what I am trying to do....

"you trying to compare the absolute values and after that the change of sign?"

Basically work out the difference then adjust the sign if nessecary....

Thanks Ian

The function I wrote returns the output you want and have stated in your examples. I was, however, not sure what you wanted with the input diff(-50, 50). Currently this outputs 100, whereas diff(50, -50) outputs -100. I think that you require the function to output 0 in this case (the absolute values are the same and all...) so I rewrote the function. This will output the values stated in your example, and for example diff(-50, 50) will output 0.

I hope this helps :)

'Code

Public Function diff(a As Integer, b As Integer) As Integer

'Declare Vars

Dim intDiff As Integer

'

intDiff = Abs(b) - Abs(a)

If a + b >= 0 Then

diff = intDiff

ElseIf a + b < 0 Then

diff = -intDiff

End If

End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

subtract = a - b

End Function