Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Check and return lowest whole number denominator

0

The best way I can describe what I'm trying to do is:

check if [cellvalue / (rangemin-rangemax)] returns a whole number from any whole number denominator within the range, starting at rangemin, and return the first/lowest denominator in that range in the formula/function cell and the divisor in the adjacent cell.

if false, subtract 1 from [cellvalue] and repeat until a whole number denominator is returned.

Assuming [value] is at B3, (rangemin) is at C3, (rangemax) is at D3, and formula/function is in E3

Example values:

[value]=77 (rangemin)=20 (rangemax)=49 would return false and subtract 1 to make [value]=76

[newvalue]=76 (range 20-49) would return "38" in E3 and "2" in F3

Answer
Discuss

Discussion

Bit confused here. Is there a repeated need for this* and why are you willing to reduce your value one or more times to determine a (hopefully) nearby number with factors in the min/max range?  

* If no repeated need, you could use an online factor calculator like Factoring Calculator
John_Ru (rep: 6142) Nov 10, '20 at 6:11 pm
We teach Excel at this site. We don't do students' homwork, nor the teachers'. Don't set tasks. Ask a question.
Variatus (rep: 4889) Nov 10, '20 at 7:07 pm
Thanks @Variatus; fair point!

As you know I've returned to this site after a gap and I'm not sure how to recognise "homework" or how full an answer or solution to provide to other questions. I did ask Don the other day to give some advice on how best to change my responses (but I guess he's busy on bigger things!)
John_Ru (rep: 6142) Nov 11, '20 at 6:00 am
Sorry, I'm not looking for someone to "do the task for me." Perhaps I phrased my request wrong. I was just looking for guidance on where to start with a formula or function. I could figure all of the values manually if necessary but thought it would be beneficial to be able to have a function or set of formulas to calculate it.

I work in a print shop with two pieces of equipment with different repeat ranges that must be divisible between eachother and was hoping to create a calculator for the CSRs to optimize both machines.

I actually was able to build a table with the values in the range I needed using a string of nested =IF with nested =MOD to find whole numbers and it's far from perfect but it's was a start.

Thanks anyways. I'll keep my questions to Google from now on.
wolffthefourth Nov 11, '20 at 8:24 am
I can't reply at present but this could be done in Excel (as you did in cells with the If and Mod functions but also as an array formula), by macro or by User Defined Function (you may have seen lessons on UDFs on this site).

Given the two machines, have you checked if the GCD worksheet function on Excel is any use?
John_Ru (rep: 6142) Nov 11, '20 at 10:43 am
Add to Discussion

Answers

0

Further to the dicussion, here's a revised VBA solution (attached file), c.f. file posted 11 Nov (and revised answer text is in italics):

In sheet 1, there are three cells (yellow filled) to complete, as per your example. Press Ctrl + Shift + J and the code should run and give you an output message (blank if no divisions are found). It first tries the value in the worksheet, sets a flag if values are found (then it drops out) or tries up to 4 lower values seeking matches. The spreadsheet is sent out with a narrow min-max range to show this (Value 73, range 20 -23) but it should work with other ranges.

The code is commented to show roughly how it works (again using MOD and IF operators). I've made no checks on the value of the parameters. It automatically reduces the Value up to 4 times (in a Do/ Loop Until) if no divisions are found. You need to do that sanity check on values manually for now.

Hope this helps.

Sub Find_Repeats()
'
' Find_Repeats Macro
'
' Keyboard Shortcut: Ctrl+Shift+J
'
Dim myVal As Long, RngMin As Long, RngMax As Long
Dim myMess As String, myCount As Integer, Falg As Boolean

'set initial values from spreadsheet
myVal = Sheet1.Cells(2, 1)
RngMin = Sheet1.Cells(2, 2)
RngMax = Sheet1.Cells(2, 3)

Do ' repeat the following until the Loop test is met

myMess = myMess & "In-range whole divisions found for " & myVal & " are:" & Chr(13) 'set up the message for this myVal

For myCount = 2 To RngMax 'start from 2 and run up to upper end of range

 If (myVal Mod myCount) = 0 And myVal / myCount >= RngMin And myVal / myCount <= RngMax Then 'if other value is a factor of Value and in range
    myMess = myMess + Str(myCount) & " x " & Str(myVal / myCount) & Chr(13) 'add this multiplication to output string
    Flag = True 'set Flag to show match(es) found
  End If
  Next myCount

myVal = myVal - 1 ' Reduce myVal by 1 before doing next loop
  
Loop Until Flag = True Or myVal <= Sheet1.Cells(2, 1) - 5 'test if a match was found or 5 attempts have been made

MsgBox myMess
 
End Sub
 
Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login