1 Replies - 702 Views - Last Post: 14 April 2011 - 11:03 PM Rate Topic: -----

#1 suspect0908  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-April 11

Problem: Cannot Input Result into Column B of Spreadsheet

Posted 13 April 2011 - 02:54 PM

Question:

Write a sub that finds the first n prime numbers, where you can choose n, and lists them in column B of Sheet1 of the Primes.xlsm file.

CODE:

Sub PrimeCountSheet1()

Dim i, j, n, p, pn As Integer
Dim prime As Integer
Dim str As String
Dim Count As Integer

str = ""
n = InputBox("Enter how many Prime Number to Display")
If (n > 50) Then
MsgBox ("Cannot Compute")
Exit Sub
End If
pn = 0



prime = 1
For i = 2 To 10000
If i = 2 Or i = 3 Then
pn = pn + 1
str = str + CStr(i) + ","
Else

For j = 2 To Sqr(i)
If (i Mod j = 0) Then
prime = 0
GoTo 200
End If
p = i
Next
200: If prime = 1 Then
pn = pn + 1
str = str + CStr(i) + ","

End If
prime = 1
End If
If (pn = n) Then
Exit For
End If


Next
[B1].Offset(Count, 0).Value = str

Count = Count + 1

End Sub



The part that isn't working for me is the

Next
[B1].Offset(Count, 0).Value = str

Count = Count + 1

End Sub




Anyone knows how to correct this?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem: Cannot Input Result into Column B of Spreadsheet

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Problem: Cannot Input Result into Column B of Spreadsheet

Posted 14 April 2011 - 11:03 PM

Sheets(1).cells(count,2).value = whatever you want


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1