Consulting

Results 1 to 11 of 11

Thread: Show Random Numbers Looping on a UserForm

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location

    Show Random Numbers Looping on a UserForm

    Hi All,
    Does anyone know if it's possible to show Random Numbers Looping on a UserForm.
    I'd like to show random numbers being generated using a Do Loop on a userform.

    is this possible?
    Thanks,
    Jim

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    It's possible

    This is in standard module, and there's a command button on the user form to stop it (attachment)

    You'll need to integrate into your code of course


    Option Explicit
    
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Public StopRand As Boolean
    
    Sub StartRand()
        Dim R As Double
        
        StopRand = False
        
        Load UserForm1
        UserForm1.Show vbModeless
        
        R = Rnd
        
        Do
            UserForm1.Label1.Caption = Format(R, "0.000000")
            UserForm1.Repaint
            DoEvents
            R = Rnd
            Sleep 500
        Loop Until StopRand
    
        UserForm1.Hide
        Unload UserForm1
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location

    Smile

    Hi Paul,
    Thank you.
    This is exactly what I was looking for.
    One last question. Is it possible to update a graph (in the userform beside these random numbers). Can a graph (i.e. histogram) dynamically change or update based on these random numbers all within the same userform?

    Thanks,
    Jim

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Is it possible to update a graph (in the userform beside these random numbers). Can a graph (i.e. histogram) dynamically change or update based on these random numbers all within the same userform?
    Don't think so unless you updated the graph on a sheet, saved it as a picture, and then updated the image on the user form .... maybe
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    You can fake a Graph with tall, Skinny Labels (Vertical Bars)

    Place them in a Frame for easier math

    Each Chart Bar (Label) Height would be set by its Random Number and its Top Property would then be Frame Height - Bar Height

    For horizontal Bars, it's easier then that. All Lefts are fixed and Bar Widths are a function of the Random Numbers.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    Thanks Paul and Sam.

    That would be very cool to do but it's beyond my expertise a little. The random numbers looping on the userform will work for now. I guess I can show the same random numbers on a sheet and show the dynamic graph update with each random change.

    Thanks again.
    I would be no where without this help. Thx.
    Jim

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Paul's Form with Fake Graph
    Attached Files Attached Files
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    Thanks Sam. This is neat.

    I was successful modifying the userform to add more labels and bars. But I wasn't able to make the bars go up and done (versus side to side).
    Lets say I wanted to make the bars go up and down and make the chart look like a bell curve....so that the bars lengths would have a maximum (shaped like a bell curve), with the random number labels looping underneath each bar.

    Could you help show that? I was having difficultly getting the bars to move up and down.

    Thanks,
    Jim

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Bell Curve with random values? That ain't gonna happen in this world.

    Place the Bars labels in a Frame. Under the Frame, organize the values Labels.

    Add this line to the beginning of the StartRandom sub
    Dim MaxHeight as Double
    Under the With UserForm1 line, add
    MaxHeight = .Controls("Frame1").Height
    Inside the For Each loop, replace .Controls("Bar" & i).Width = 100*R with
    With .Controls("Bar" & i)
       .Height = R * MaxHeight
       .Top = MaxHeight - (1 - R)
    End with
    Note that I did not redownload the workbook, so I am working from memory.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Try this version

    Capture.JPG

    You'll have to play with the sizes to get the legibility you want
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  11. #11
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    This is really really cool. Thanks Paul

Posting Permissions

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