View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default Finding all divisors of given number in a set

You could try this... I have assumed that the list is on sheet1 and that N is
in Cell B1 on that sheet. Change to suit.

You could also use Conditional Formatting without any code to just highlight
the values if you wanted.

Sub Divisors()
Dim wksToSearch As Worksheet
Dim rngToSearch As Range
Dim lngNumerator As Long
Dim rng As Range
Dim wksNew As Worksheet
Dim rngPaste As Range

Set wksNew = Worksheets.Add
On Error Resume Next
wksNew.Name = "Denominators" 'try to rename sheet
On Error GoTo 0
Set rngPaste = wksNew.Range("A1")

Set wksToSearch = Sheets("Sheet1")
With wksToSearch
Set rngToSearch = .Range(.Range("A1"), .Cells(Rows.Count, "A").End(xlUp))
End With

lngNumerator = wksToSearch.Range("B1").Value

For Each rng In rngToSearch
If lngNumerator Mod rng.Value = 0 Then
rngPaste.Value = rng.Value
Set rngPaste = rngPaste.Offset(1, 0)
End If
Next rng
End Sub
--
HTH...

Jim Thomlinson


"Mac" wrote:

Hello,

is there an algorithm for this:
In a column, say A, I have some 150 numbers; then I have one number N and I
need to find all divisors of number N in column A and have them ''extracted'
to a list, or marked, or whatever. Is there a piece of code? Thank you!
Mac