JBShandrew
Board Regular
- Joined
- Apr 17, 2011
- Messages
- 54
Hi VBA experts. The code below was found on Microsoft's website under VBA for 2003. It is working just fine in Excel 2007. The only thing I would like to change is to have this code execute based on a cell value. For Example I would like the macro to execute one time when a value is placed H2, and not execute again until the value in H2 is changed.
This is a very quick way to get prime numbers for a given number, and is much better than using my calculator to do the division over and over.
Thank you in advance.
This is a very quick way to get prime numbers for a given number, and is much better than using my calculator to do the division over and over.
Thank you in advance.
Code:
Sub GetFactors()
Dim Count As Integer
Dim NumToFactor As Single 'Integer limits to < 32768
Dim Factor As Single
Dim y As Single
Dim IntCheck As Single
Count = 0
Do
NumToFactor = _
Application.InputBox(Prompt:="Type integer", Type:=1)
'Force entry of integers greater than 0.
IntCheck = NumToFactor - Int(NumToFactor)
If NumToFactor = 0 Then
Exit Sub
'Cancel is 0 -- allow Cancel.
ElseIf NumToFactor < 1 Then
MsgBox "Please enter an integer greater than zero."
ElseIf IntCheck > 0 Then
MsgBox "Please enter an integer -- no decimals."
End If
'Loop until entry of integer greater than 0.
Loop While NumToFactor <= 0 Or IntCheck > 0
For y = 1 To NumToFactor
'Put message in status bar indicating the integer being checked.
Application.StatusBar = "Checking " & y
Factor = NumToFactor Mod y
'Determine if the result of division with Mod is without _
remainder and thus a "factor".
If Factor = 0 Then
'Enter the factor into a column starting with the active cell.
ActiveCell.Offset(Count, 0).Value = y
'Increase the amount to offset for next value.
Count = Count + 1
End If
Next
'Restore Status Bar.
Application.StatusBar = "Ready"
End Sub