Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-11-2012, 06:58 AM
Jamtart Jamtart is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 64bit
Advanced Beginner
Randomly generate numbers, Macro?
 
Join Date: Apr 2012
Posts: 53
Jamtart is on a distinguished road
Default 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....
Reply With Quote
  #2  
Old 08-11-2012, 07:09 AM
JohnWilson JohnWilson is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

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


__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials
Reply With Quote
  #3  
Old 08-11-2012, 07:40 AM
Jamtart Jamtart is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 64bit
Advanced Beginner
Randomly generate numbers, Macro?
 
Join Date: Apr 2012
Posts: 53
Jamtart is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 08-11-2012, 07:49 AM
JohnWilson JohnWilson is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

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
__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials
Reply With Quote
  #5  
Old 08-11-2012, 07:54 AM
Jamtart Jamtart is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 64bit
Advanced Beginner
Randomly generate numbers, Macro?
 
Join Date: Apr 2012
Posts: 53
Jamtart is on a distinguished road
Default

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...
Reply With Quote
  #6  
Old 08-11-2012, 08:03 AM
JohnWilson JohnWilson is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

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
__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials
Reply With Quote
  #7  
Old 08-11-2012, 08:18 AM
Jamtart Jamtart is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 64bit
Advanced Beginner
Randomly generate numbers, Macro?
 
Join Date: Apr 2012
Posts: 53
Jamtart is on a distinguished road
Default

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...
Reply With Quote
  #8  
Old 08-11-2012, 08:47 AM
JohnWilson JohnWilson is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

"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
Few notes since you like learning

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)
__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials
Reply With Quote
  #9  
Old 08-11-2012, 09:03 AM
Jamtart Jamtart is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 64bit
Advanced Beginner
Randomly generate numbers, Macro?
 
Join Date: Apr 2012
Posts: 53
Jamtart is on a distinguished road
Default

Oh dear, now I am sort of lost
Quote:
from the HOME Tab > Select on the right)say you named them
Current totalResistance etc
Working on it....
Reply With Quote
  #10  
Old 08-11-2012, 09:06 AM
Jamtart Jamtart is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 64bit
Advanced Beginner
Randomly generate numbers, Macro?
 
Join Date: Apr 2012
Posts: 53
Jamtart is on a distinguished road
Default

I got it, thanks John, this is a great start to my little project...
Reply With Quote
  #11  
Old 08-11-2012, 10:18 AM
Jamtart Jamtart is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 64bit
Advanced Beginner
Randomly generate numbers, Macro?
 
Join Date: Apr 2012
Posts: 53
Jamtart is on a distinguished road
Default

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 ?
Reply With Quote
  #12  
Old 08-11-2012, 12:09 PM
JohnWilson JohnWilson is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

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)
__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials
Reply With Quote
  #13  
Old 08-11-2012, 01:50 PM
Jamtart Jamtart is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 64bit
Advanced Beginner
Randomly generate numbers, Macro?
 
Join Date: Apr 2012
Posts: 53
Jamtart is on a distinguished road
Default

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.
Attached Files
File Type: zip series circuit 3.zip (287.4 KB, 18 views)
Reply With Quote
  #14  
Old 08-11-2012, 06:22 PM
Jamtart Jamtart is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 64bit
Advanced Beginner
Randomly generate numbers, Macro?
 
Join Date: Apr 2012
Posts: 53
Jamtart is on a distinguished road
Default

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:
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)
Been trying to add this to my finished project but with no luck. I want to be able to change the Applied Voltage or the Resistance and still get the correct results. The math part I should be able to figure out, just cant get the second (voltage variable) to work
Also, is there a good resource (ebook?) for me to get to help me learn VBA for Powerpoint?
Reply With Quote
  #15  
Old 08-11-2012, 11:44 PM
JohnWilson JohnWilson is offline Randomly generate numbers, Macro? Windows 7 64bit Randomly generate numbers, Macro? Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

See if this helps

In show mode change the new textbox and hit enter.

RIGHT click the new textbox to see the code
__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials
Reply With Quote
Reply



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
Randomly generate numbers, Macro? 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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft