-6

I have a value in any cell which is greater than 60. For my purpose, I need to subtract 60 from such values until the value is less than 60. So, for example, say I have a cell containing a value of 1000. The value I want to use would be 40 because 40 is the first value that is less than 60 after repeatedly subtracting 60.

I envision a VBA solution that would subtract 60 repeatedly and then stop and display the final value, 40, as this satisfies the condition.

The data range is large and values above 60 can occur anywhere in the range. I want this to be done automatically when I open the spreadsheet so that all formulas that might refer to values over 60 use the remainders.

2
  • 1
    1) If you drag your formula, none of the cells but the 1st will refer to your 1000 value; more data must be entered or you need to anchor the reference with $s. 2)Are you looking to have VBA populate the range with the formula instead of dragging it? It will take you longer to write the VBA code than to drag it manually. Even over a large range, that formula won't require much time to calculate and it will be the same in VBA. Are you looking to populate just the calculation result? You can do that by dragging a reference to the result cell. Can you clarify what you want to accomplish?
    – fixer1234
    Dec 21, 2014 at 6:59
  • HI, Thanks for the reply Yes a) i just want the calculation result to be displayed in an excel sheet in any cell. b) i would like the calculation to be automatic when i open the workbook. In this case , I have a value in any cell in excel sheet which is 1000 and as 1000 is greater than 60 i need to subtract 60 from 1000 until the value is less than 60 . While using a calculator the value which i want after subtracting is 40 because 40 is the first value which is less than 60. And also i want the calculation to stop and display Final value 40, as this satisfies the condition. Dec 21, 2014 at 16:22

2 Answers 2

0

You don't need VBA to do what you want. You want to subtract 60 repeatedly until the remainder is less than 60. You could write VBA code to do that, but you can do that in one step with a formula:

    =MOD(ED1,60)

If you divide 1000 by 60, you get 16.666... That means that you would need to subtract 60 sixteen times to get something less than 60. The MOD function produces the remainder after the division (in this case, 40), which is equivalent to those subtractions.

You can use this in two ways. One is to incorporate this MOD expression (without the equal sign) in your formulas in place of the simple cell reference to data cells that can exceed 60. This will use the translated value.

If you have numerous formulas that reference each cell and don't want to have the overhead of all of that repeated recalculation of the same values, create a set of translated values--a duplicate data range that contains the above formula. That will translate the data once and then all of your formulas can reference these numbers instead of the raw data. If you don't want the overhead of those calculations being refreshed, do a copy | paste special | values. You can paste the results over the raw data and then delete the intermediate calculations.

3
  • I did exactly the same thing, but the value comes out to be 2400 instead of 40. Is there any other workarounds using formulas,vbas, plugins etc apart from =IF(ED1>60,ED1-60,ED1)? which would be beneficial in performing larger calculations. Dec 21, 2014 at 19:02
  • My bad, not enough coffee. The MOD function, alone produces the actual remainder, you don't need the *60 at the end. I corrected the formula and discussion. I'm not sure I understand the rest of your comment. Does this correction do what you need or are you looking for something different? (Not sure what you mean by larger calculations; MOD doesn't solve by successive subtractions, it does it by division, then chopping off the whole number, whatever that is, and then converting the fractional remainder back to a whole number.)
    – fixer1234
    Dec 21, 2014 at 19:30
  • Hi, Thank you so much for opening my eyes and putting a big smile in my face. This is exactally what i was after. :::::::::::!!!!!!!!!!!:::::::::::AWESOME SOLUTION:::::::!!!!!!!!::::::::: Dec 21, 2014 at 22:13
0

I have great news! Because the value in ED1 is a constant, the formula will always result in the value:

960

Let's assume the long range is A1:Z100

Here is the VBA based solution:

Private Sub Workbook_Open()
    Sheets("Sheet1").Range("A1:Z100").Value = 960
End Sub

This is a workbook event macro.

Because it is workbook code, it is very easy to install and use:

  1. right-click the tiny Excel icon just to the left of File on the Menu Bar
  2. select View Code - this brings up a VBE window
  3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the macro:

  1. bring up the VBE windows as above
  2. clear the code out
  3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

To learn more about Event Macros (workbook code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

Macros must be enabled for this to work!

3
  • Hi, thanks for the reply, actually the value iam looking for is 40 because when i start subtracting 60 from 1000 until the value is less than 60 is "40" and also i want that value to be displayed in any cell in excel sheet after the calculation is done. Dec 21, 2014 at 16:04
  • @EXCELLOVER Then modify my posted code to meet your exact needs! Dec 21, 2014 at 16:31
  • Well, Gary's Student That won't be needed now. Thanks for your effort, i do appreciate it Dec 21, 2014 at 22:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .