VBA find the 2 closed numbers that a factors of my number

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,411
Looking for some code that will return the closes 2 numbers that are factors of a given number.

given number 12: the 2 factor that are the closest to each other are 3 & 4 (not 2 & 6)

24 would return 6 & 4 (not 8 & 3 or 2 & 12)
64 would return 8 & 8 (not 16 & 4)
90 would be 9 & 10
37 would return 1 & 37
....

ex:
x= input(Type your number)
...code...

msgbox ("highest factors are " & F1 & " and " & F2)


Thanks for looking
Ross
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Code:
Function CloseFactors(n As Long) As String
  Dim i             As Long

  For i = Sqr(n) To 1 Step -1
    If n Mod i = 0 Then
      CloseFactors = i & ", " & n / i
      Exit For
    End If
  Next i
End Function

[Table="width:, class:grid"][tr][td="bgcolor:#C0C0C0"][/td][td="bgcolor:#C0C0C0"]
A​
[/td][td="bgcolor:#C0C0C0"]
B​
[/td][td="bgcolor:#C0C0C0"]
C​
[/td][/tr][tr][td="bgcolor:#C0C0C0"]
1​
[/td][td]
55​
[/td][td]5, 11[/td][td]B1: =CloseFactors(A1)[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
2​
[/td][td]
56​
[/td][td]7, 8[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
3​
[/td][td]
57​
[/td][td]3, 19[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
4​
[/td][td]
58​
[/td][td]2, 29[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
5​
[/td][td]
59​
[/td][td]1, 59[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
6​
[/td][td]
60​
[/td][td]6, 10[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
7​
[/td][td]
61​
[/td][td]1, 61[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
8​
[/td][td]
62​
[/td][td]2, 31[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
9​
[/td][td]
63​
[/td][td]7, 9[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
10​
[/td][td]
64​
[/td][td]8, 8[/td][td][/td][/tr]
[/table]
 
Last edited:
Upvote 0
Give this function a try...
Code:
Function NearestFactors(Num As Long) As String
  Dim X As Long
  For X = Int(Sqr(Num)) To 1 Step -1
    If Num Mod X = 0 Then
      NearestFactors = X & " and " & Num / X
      Exit Function
    End If
  Next
End Function
 
Upvote 0
You could try this code - some data validation is built in:

Code:
Sub ClosestFactors()    
    Num = Application.InputBox("Enter a positive integer", "Enter number", , , , , , 1)
    If Num <> Int(Num) Or Num <= 0 Then GoTo BadInput
    num1 = Int(Sqr(Num))
    For i = num1 To 1 Step -1
    If Num Mod i = 0 Then
    MsgBox ("Closest factors are " & i & " and " & Num / i)
    Exit Sub
    End If
    Next
BadInput:
    MsgBox ("Invalid input"): Exit Sub
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
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