|
#1
|
|||
|
|||
Randomly generate numbers, Macro?
Ok, so I have got this idea for a powerpoint and I need some help getting it started. I want to be able to hit a button and a text box will generate a random number. Lets say the parameters would be between 10 and 1000.
This would give me a jump on a larger project that I have in mind. Thanks for any assistance here.... |
#2
|
|||
|
|||
It needs a macro
To generate the random number use this formula: Number =Int((highest number in range - one less that lowest number)*rnd + lowest number in range)
EXAMPLE number between 10 and 1000 Code:
Number=Int(991*rnd)+10 '991 is 1000 -9 |
#3
|
|||
|
|||
Hi John, I did find this one and it works great:
Sub UpdateRandomNumber(oSh As Shape) oSh.TextFrame.TextRange.Text = CStr(Random(12)) SlideShowWindows(1).View.GotoSlide (SlideShowWindows(1).View.Slide.SlideIndex) End Sub Function Random(High As Long) As Long Randomize Random = Int((High * Rnd) + 1) End Function No I need to find a way to get this randomly generated number to interact with other numbers. This is an electrical exersize and the formulas are based on Ohms law. The circuit will have 120 V applied, and two resistors in series, the reistance of one resistor is the Randomly generated number and the other I would keep at 100 ohms at this time. so the formulas/results that I would be looking to have work on the screen would be: 1. Total resistance - Randomly generated number plus 100 2. Current - 120 divided by Total resistance 3. Volt drop 1, which is the resistor having the Randoml generated number - Current times the randomly generated number 4. Volt drop 2, which is my static resistor of 100 ohms - Current time 100 I am trying to learn VBA and this would go a long way to doing that as well as a spring board to other similiar projects. |
#4
|
|||
|
|||
Note though that will generate a number between 1 and 12.
To get what you said you would need to pass the High and the Low values Code:
Sub UpdateRandomNumber(oSh As Shape) oSh.TextFrame.TextRange.Text = CStr(Random(100, 25)) SlideShowWindows(1).View.GotoSlide (SlideShowWindows(1).View.Slide.SlideIndex) End Sub Function Random(High As Long, Low As Long) As Long Randomize Random = Int((High - (Low - 1)) * Rnd) + Low End Function |
#5
|
|||
|
|||
Oh ya, I changed the 12 to a 1000 in that script...
Any thoughts as to how to accomplish the other items? Working on it here but my VBA is so lacking... |
#6
|
|||
|
|||
Here's the basics
Code:
Sub UpdateRandomNumber() Dim Randval As Long Dim current As Single Dim voltDrop1 As Single Dim voltDrop2 As Single Dim ResTotal As Single Randval = Random(1000, 10) ResTotal = Randval + 100 current = 120 / ResTotal voltDrop1 = current * Randval voltDrop2 = current * 100 MsgBox "Total resistance = " & CStr(ResTotal) & vbCrLf & _ "Current = " & CStr(current) & vbCrLf & _ "Volt Drop 1 = " & CStr(voltDrop1) & vbCrLf & _ "Volt Drop 2 = " & CStr(voltDrop2) End Sub Function Random(High As Long, Low As Long) As Long Randomize Random = Int((High - (Low - 1)) * Rnd) + Low End Function |
#7
|
|||
|
|||
Oh man, I wasnt even close Thats awesome John. So, if I wanted to have the results displayed in seperate shape like a rectangle that I place, ie total resistance in one shape, current in another, then I would have to create seperate Modules in the VBA window of the presentation?
I think this is great, love learning about this stuff... |
#8
|
|||
|
|||
"I would have to create seperate Modules in the VBA window of the presentation?"
Probably not. In 2010 you can name each shape in the selection pane (from the HOME Tab > Select on the right)say you named them Current totalResistance etc Code:
Sub UpdateRandomNumber() Dim Randval As Long Dim current As Single Dim voltDrop1 As Single Dim voltDrop2 As Single Dim ResTotal As Single Dim osld As Slide Randval = Random(1000, 10) ResTotal = Randval + 100 current = 120 / ResTotal voltDrop1 = current * Randval voltDrop2 = current * 100 Set osld = SlideShowWindows(1).View.Slide 'The current slide osld.Shapes("Current").TextFrame.TextRange = "The current is = " & CStr(current) osld.Shapes("totalResistance").TextFrame.TextRange = "The total resistance is = " & CStr(ResTotal) 'etc End Sub Function Random(High As Long, Low As Long) As Long Randomize Random = Int((High - (Low - 1)) * Rnd) + Low End Function I used the type SINGLE for my variables. This is just in case you had decimal results. In your case I don't think that's possible and you could use LONG. osld is an OBJECT variable (in this case a slide) With object variables you must use the SET word you cannot say osld=SlideShowWindows(1).View.Slide Cstr converts a number to text PowerPoint will usually do this for you and many people regard the Cstr as not needed. I would always use it. I don't trust usually (well not usually) |
#9
|
|||
|
|||
Oh dear, now I am sort of lost
Quote:
|
#10
|
|||
|
|||
I got it, thanks John, this is a great start to my little project...
|
#11
|
|||
|
|||
One last thing I would like to know, what if I wanted to generate specific variables, such as 30, 40, 50, etc up to 120 ?
|
#12
|
|||
|
|||
Do you mean a random choice of 30, 40, 50 etc??
Easiest way would be to gererate 3 - 12 and multiply the number generated by 10 Randval = 10 * Random(3, 12) |
#13
|
|||
|
|||
Here is my first one done, just need to send to the staff for feedback. Thanks for all your help John. This will be a good start for more to come.
|
#14
|
|||
|
|||
Ok, so I created some nice variations of the completed project. But now I am tring to cmplicate it up a bit. I want to be able to have different voltages as a variable, hence my earlier post and johns response:
Quote:
Also, is there a good resource (ebook?) for me to get to help me learn VBA for Powerpoint? |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to recap data of same numbers | Jasa P | Word VBA | 0 | 06-28-2012 02:41 AM |
Formatting changing randomly | Franuzz | Word | 1 | 07-14-2011 04:12 PM |
black text randomly changing to white when run macro | G DeBree | PowerPoint | 0 | 03-16-2011 09:23 AM |
Randomly delayed emails | Colinest84 | Outlook | 1 | 01-14-2011 02:32 AM |
Documents Randomly Corrupting | the IT Guy | Word | 0 | 02-06-2009 09:11 AM |