+ Reply to Thread
Results 1 to 3 of 3

Thread: Loop not looping

  1. #1
    Registered User
    Join Date
    03-16-2010
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2003
    Posts
    1

    Loop not looping

    I am a newby to VBA and below I am trying to generate a random number between 3000 and 3777, but it doesn't work. It only goes through once then quits producing all kinds of numbers outside the desired range. Here is my code:

    Option Explicit


    Sub Enter_Click()

    Dim BCN As Integer
    Dim BCN1 As Integer
    Dim BCN2 As Integer

    Do
    Range("D6") = Rnd() * 10000
    Loop Until Range("D6").Value >= 3000 <= 3700
    BCN = ActiveSheet.Range("D6").Value
    ActiveSheet.Shapes("BCN1").Select
    Selection.Characters.Text = BCN
    ActiveSheet.Shapes("BCN2").Select
    Selection.Characters.Text = BCN

    ' the above code is supposed to loop until a random number between
    ' 3000 and 3777 is produced, but it doesn't work. It only goes
    ' through once as if the do loop isn't even there, then quits.

    End Sub

    What am I doing wrong??

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    MS 2007
    Posts
    5,372

    Re: Loop not looping

    Your Loop Until line isn't correct
    Loop Until Range("D6").Value >= 3000 And Range("D6") <= 3700
    You could try the RANDBETWEEN Function instead and skip the Do loop
    Range("D6") = WorksheetFunction.RandBetween(3000, 3700)
    Lastly, if you're expecting Shapes("BCN1") to convert to Shapes(32141) (or somesuch), it won't because the BCN is inside quotation marks and won't be seen as a variable. Hope that helped.
    ChemistB
    My 2¢

    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

  3. #3
    Forum Guru 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Vienna, VA, USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2,257

    Re: Loop not looping

    Quote Originally Posted by bg_ac_dziner View Post
    Loop Until Range("D6").Value >= 3000 <= 3700
    I'm surprised that even compiles. Does anyone know how that expression is evaluated?
    Making the world a better place one fret at a time
    ||||||

    If someone helped you, please click on the star icon at the bottom of their post

    If your problem is solved, please update the first post:
    EDIT, Go Advanced button, set Prefix to SOLVED

    [code]
    ' Enclose code in tags like this
    [/code]

    Don't attach a screenshot
    --just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0