Formula to find exact division

Beneindias

Board Regular
Joined
Jun 21, 2022
Messages
120
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi guys,

I'm trying to find a formula that will find the second biggest number that can be divided by another number, with remainder of 0.

Basically, I want to do an exact division with the second biggest number.
Is there any formula to find this number?

For example:

4 is divided by 1, 2 and 4
I need to get 2

6 is divided by 1, 2, 3 and 6
I need to get 3

8 is divided by 1, 2, 4 and 8
I need to get 4

9 is divided by 1, 3, 9
I need to get 3


I hope that I'm explaining this in a way that you can understand, because it's much easier in my language xD

Thank you all
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about
Fluff.xlsm
LM
1
263
384
4155
571
Sheet5
Cell Formulas
RangeFormula
M2:M5M2=LET(s,SEQUENCE(L2-1),TAKE(SORT(FILTER(s,MOD(L2,s)=0),,-1),1))
 
Upvote 0
Solution
Here is a User Defined Function we can place in VBA, and then use as we would any native Excel function:
VBA Code:
Function SecondDivisor(num As Long) As Long

    Dim x As Long
    
'   Make sure num is not 0
    If num = 0 Then
        SecondDivisor = 0
        Exit Function
    End If
    
'   Loop through numbers backwards
    For x = (num - 1) To 1 Step -1
'       See if no remainder when dividing by x
        If (num / x) = Int(num / x) Then
            SecondDivisor = x
            Exit Function
        End If
    Next x
    
'   If no values found, choose 1
    SecondDivisor = 1
    
End Function

So, if we have a number in cell J1, the formula would be:
Excel Formula:
=SecondDivisor(J1)

Here is a sample of results:
1695223109338.png
 
Upvote 0
How about
Fluff.xlsm
LM
1
263
384
4155
571
Sheet5
Cell Formulas
RangeFormula
M2:M5M2=LET(s,SEQUENCE(L2-1),TAKE(SORT(FILTER(s,MOD(L2,s)=0),,-1),1))
Thanks @Fluff.

Your solution worked.
I didn't tried the other ones, because your way seemed simpler and I was avoiding the use of VBA in this project.

Thank you three for the help in this.
 
Upvote 0
Here is a User Defined Function we can place in VBA, and then use as we would any native Excel function:
VBA Code:
Function SecondDivisor(num As Long) As Long

    Dim x As Long
   
'   Make sure num is not 0
    If num = 0 Then
        SecondDivisor = 0
        Exit Function
    End If
   
'   Loop through numbers backwards
    For x = (num - 1) To 1 Step -1
'       See if no remainder when dividing by x
        If (num / x) = Int(num / x) Then
            SecondDivisor = x
            Exit Function
        End If
    Next x
   
'   If no values found, choose 1
    SecondDivisor = 1
   
End Function

So, if we have a number in cell J1, the formula would be:
Excel Formula:
=SecondDivisor(J1)

Here is a sample of results:
View attachment 98989
Thank you.

I didn't used your solution, because i'm trying to avoid the usage of VBA in this project.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Looking at @Fluff's solution made me want to update the LAMBDA. It handles splitting the factors out to rows or columns with the optional 'to_col' argument.

FACTORS
ABCD
14689
22343
31221
411
5
6421
76321
88421
9931
Sheet1
Cell Formulas
RangeFormula
A2:A3,D2:D3,B2:C4A2=FACTORS(A1)
B6:C6,B9:C9,B7:D8B6=FACTORS(A6,1)
Dynamic array formulas.


Excel Formula:
=LAMBDA(number,[to_col],
    LET(
        s,SEQUENCE(
            (to_col-1)*(-number+2)+1,
            to_col*(number-2)+1
        ),
        SORT(FILTER(s,MOD(number,s)=0),,-1,to_col)
    )
)
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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