View Full Version : [SOLVED:] Show Random Numbers Looping on a UserForm
Sully1440
03-07-2018, 04:35 PM
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
Paul_Hossler
03-07-2018, 05:46 PM
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
Sully1440
03-08-2018, 07:48 AM
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
Paul_Hossler
03-08-2018, 08:15 AM
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
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.
Sully1440
03-08-2018, 09:50 AM
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
Paul's Form with Fake Graph
Sully1440
03-08-2018, 01:21 PM
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
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.
Paul_Hossler
03-08-2018, 05:05 PM
Try this version
21775
You'll have to play with the sizes to get the legibility you want
Sully1440
03-09-2018, 12:27 PM
This is really really cool. Thanks Paul :)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.