Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-09-2013, 10:10 PM
Sajj Sajj is offline Filling cells with random numbers using loops? (VBA) Windows 8 Filling cells with random numbers using loops? (VBA) Office 2007
Novice
Filling cells with random numbers using loops? (VBA)
 
Join Date: Apr 2013
Posts: 3
Sajj is on a distinguished road
Default Filling cells with random numbers using loops? (VBA)

Hello,



I need to write a mcro that fills all the cells in the block range of cell with random numbers between 0 and 1.

so far I've done this:
Sub number()
Dim x As Double



For x = 1 To 10


Workbooks("book1").Sheets("Sheet1").Range("A1:f20" ).Offset(x, j).Value = Rnd()


Next
End Sub

the problem with my macro is having one random number on almost the entire determined range.
Reply With Quote
  #2  
Old 04-09-2013, 11:43 PM
macropod's Avatar
macropod macropod is offline Filling cells with random numbers using loops? (VBA) Windows 7 64bit Filling cells with random numbers using loops? (VBA) Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Why do you need a macro, when the =RAND() worksheet function can achieve the same result - and change it every time to workbook updates?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-10-2013, 10:00 AM
Sajj Sajj is offline Filling cells with random numbers using loops? (VBA) Windows 8 Filling cells with random numbers using loops? (VBA) Office 2007
Novice
Filling cells with random numbers using loops? (VBA)
 
Join Date: Apr 2013
Posts: 3
Sajj is on a distinguished road
Default

I wanted to assign a macro to any shape in order to give set of different random numbers between 0&1 just to make it easier for any one else.For that, I'm sure that I have to use the For next loop.
Reply With Quote
  #4  
Old 04-10-2013, 11:56 AM
Sajj Sajj is offline Filling cells with random numbers using loops? (VBA) Windows 8 Filling cells with random numbers using loops? (VBA) Office 2007
Novice
Filling cells with random numbers using loops? (VBA)
 
Join Date: Apr 2013
Posts: 3
Sajj is on a distinguished road
Default

got it

Sub randoms()

Dim randoms As Range
Range("a1:f20").Select
For Each randoms In Selection
object.Value = Rnd()
Next

End Sub
Reply With Quote
  #5  
Old 04-10-2013, 02:29 PM
macropod's Avatar
macropod macropod is offline Filling cells with random numbers using loops? (VBA) Windows 7 64bit Filling cells with random numbers using loops? (VBA) Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

There is no need to select anything. Also, in one place your code refers to 'randoms', but then uses a different name (object) to output the value. Such inconsistencies can lead to serious problems in a more complex macro. Try:
Code:
Sub Randoms()
Dim Rng As Range
For Each Rng In Range("A1:F20")
  Rng.Value = Rnd()
Next
End Sub
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to fill a cell with random numbers to do calculations? zanat0s Excel 1 06-13-2011 10:33 AM
Filling cells with random numbers using loops? (VBA) filling multiple cells whislt using Filter VinceO Excel 1 05-09-2011 06:38 AM
Display Random Numbers as Transition greenbutton PowerPoint 0 03-09-2011 04:43 PM
Filling cells with random numbers using loops? (VBA) Powerpoint loops, but not to beginning imeister PowerPoint 1 02-02-2011 02:05 PM
loops within presentations supateach PowerPoint 1 11-23-2010 04:42 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:11 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