2013-10-23 17 views

回答

0

這給一試:

Sub MAIN() 
    Dim ss As Single, i As Single 
    ss = Application.InputBox(Prompt:="Enter and integer", Type:=1) 
    k = 1 
    For i = ss To 1 Step -1 
     If IsPrime(i) Then 
      Cells(k, 1) = i 
      k = k + 1 
     End If 
    Next 
End Sub 

Function IsPrime(Num As Single) As Boolean 
    Dim i As Long 
    If Num < 2 Or (Num <> 2 And Num Mod 2 = 0) _ 
     Or Num <> Int(Num) Then Exit Function 
    For i = 3 To Sqr(Num) Step 2 
     If Num Mod i = 0 Then Exit Function 
    Next 
    IsPrime = True 
End Function 

來源:

http://dailydoseofexcel.com/archives/2005/06/30/is-this-number-prime/

編輯#1

要使用一個MsgBox輸出結果,使用這個主:

Sub MAIN() 
    Dim ss As Single, i As Single 
    Dim s As String 
    ss = Application.InputBox(Prompt:="Enter and integer", Type:=1) 
    k = 1 
    For i = ss To 1 Step -1 
     If IsPrime(i) Then 
      s = s & "," & i 
     End If 
    Next 
    MsgBox s 
End Sub 

但一個MsgBox在他們多少材料顯示限制。

+0

您正確地得到了我的問題,但如果必須在消息框上的同一行上打印所有這些數字,該怎麼辦? –

相關問題