Macro to compare results of multiple subtractions

normanblu

New Member
Joined
Jul 23, 2015
Messages
3
I want to write a macro on excel to subtract a number from many different data points and compare the results. For example, 3 of the data points are 3400, 1700, and 850. If the the number is 1200, I want to write a macro that will find 3400 - 1200, 1700 - 1200, and 850 - 1200 and then determine which has the smallest positive difference. A snag is that I want to discard 850- 1200 because the answer is negative, so the correct answer would be 1700 - 1200.

I'm stuck and not even sure how to go about starting, any help would be greatly appreciated.
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You don't need a macro

Try array formula

= MIN(IF(#Range>=#,#Range)) confirmed with CTRL+SHIFT+ENTER (not just ENTER)

or if you are trying to find the smallest positive difference

= MIN(IF(#Range>=#,#Range-#)) confirmed with CTRL+SHIFT+ENTER (not just ENTER)

If you insist on a macro

Code:
Sub testing()


MsgBox Application.Evaluate("=MIN(IF(A1:A10>=B1,A1:A10-B1))")


End Sub
 
Last edited:
Upvote 0
Thank you so much for your suggestion, ndsutherland. The reason I want a macro is because in reality there are about 20 data points and I will be using this on many different spreadsheets.
 
Upvote 0
This is a little more robust

Code:
Sub testing()
Dim rrng As Range
Dim ws As Worksheet
Dim nums As String
Dim snum As String
Set rrng = Application.InputBox("Select the range of numbers", , , , , , , 8)
Set ws = rrng.Worksheet
nums = rrng.Address
snum = InputBox("Enter the number to subtract")


MsgBox Application.Evaluate("=MIN(IF(" & nums & ">=" & snum & ",IF(ISNUMBER(" & nums & ")," & nums & "-" & snum & ")))")


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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