• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

7. Macro Hacks > 68. Generate Random Numbers

Generate Random Numbers

Randomization is an esoteric area of most programming languages, and VBA’s no exception. This hack gives you easy access to two of its most common forms.

Random numbers are a helpful way to ensure that a particular value, such as a bookmark name for a cross-reference [Hack #44] , is unique. Word even uses random numbers in its own cross-reference bookmarks. But getting a random number from VBA in the form you need can be a challenge.

The first time you need a random number from VBA, you’ll look up the syntax in the help files, and then you’ll have to figure out a way to get the kind of number you need. The second time you need a random number—probably months later—chances are you’ll have long since forgotten the syntax, and you’ll spend needless time looking it up again and trying to figure out how to coax the kind of number you need from it. The functions in this hack can help you save yourself the aggravation.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint