+ Reply to Thread
Results 1 to 8 of 8

Tutorial for populating a 100x100 array with a random number between 1 and X

Hybrid View

  1. #1
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Post Tutorial for populating a 100x100 array with a random number between 1 and X

    Hi there all,

    I'm looking to see if anyone can point me towards a tutorial or set of tutorials that would help me create and populate a vba 100x100 array with a random whole number between 1 and X, where X is likely 32 or somewhere near it, but I've not yet finalised.

    The answer to this would be lovely, but I think i'd learn more if anyone could guide me.... I've searched a number of tutorials on arrays, and will keep doing so, but being completely new to arrays, it's tough going and I wondered if anyone knows of any particularly good tutorials...


    Thanks in advance...

    Darth
    Last edited by Darth269; 04-11-2017 at 06:03 AM. Reason: Solved

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,636

    Re: Tutorial for populating a 100x100 array with a random number between 1 and X

    Use

    Formula: copy to clipboard
    =RANDBETWEEN(1, 32)

    and pull formula to fill all array.

    Then copy -> paste as values if you don't wan them to change.

  3. #3
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Re: Tutorial for populating a 100x100 array with a random number between 1 and X

    Quote Originally Posted by zbor View Post
    Use

    Formula: copy to clipboard
    =RANDBETWEEN(1, 32)

    and pull formula to fill all array.

    Then copy -> paste as values if you don't wan them to change.

    Hi there and thanks for the reply... however unless i'm missing something, this doesn't help with a VBA Array... I guess I should have specified, but I thought using the term Array, and posting in the Macro forum would make it clear. I do appreciate the response though, and don't want to sound ungrateful, this just isn't what I'm looking for.

  4. #4
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,636

    Re: Tutorial for populating a 100x100 array with a random number between 1 and X

    People often put question in a wrong subforum so it's nice to write desired solution

    Here, try this:

    Sub Random_Array()
    
    Set Rng = Range("A1")
    For i = 1 To 100
        For j = 1 To 100
            Rng.Offset(i - 1, j - 1).Value = 1 + Int(32 * Rnd())
        Next j
    Next i
    End Sub
    Last edited by zbor; 04-11-2017 at 05:36 AM.

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Tutorial for populating a 100x100 array with a random number between 1 and X

    Create Array in memory and write to worksheet in 1 go.

    Sub Random_Array()
    Dim square()
    numrows = 20: numcols = 10
    ReDim square(1 To numrows, 1 To numcols)
    For i = 1 To numrows
        For j = 1 To numcols
            square(i, j) = 1 + Int(32 * Rnd())
        Next j
    Next i
    With Range("A1")
        .CurrentRegion.ClearContents
        .Resize(numrows, numcols) = square
    End With
    End Sub
    Last edited by bakerman2; 04-11-2017 at 05:36 AM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Re: Tutorial for populating a 100x100 array with a random number between 1 and X

    Quote Originally Posted by bakerman2 View Post
    Create Array in memory and write to worksheet in 1 go.

    Sub Random_Array()
    Dim square()
    numrows = 20: numcols = 10
    ReDim square(1 To numrows, 1 To numcols)
    For i = 1 To numrows
        For j = 1 To numcols
            square(i, j) = 1 + Int(32 * Rnd())
        Next j
    Next i
    With Range("A1")
        .CurrentRegion.ClearContents
        .Resize(numrows, numcols) = square
    End With
    End Sub

    Hi there Bakerman2,

    thanks for that, I think i've come up with almost what you've written, but i'm getting an error when running, can you spot where I've gone wrong?

    Sub MAP_CREATOR()
    
    Dim MapArray() As Byte
    Dim SysType As Byte
    Dim TheRange As Range
    Dim CellsDown As Long, CellsAcross As Long
    
    
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    '   Change these values
    CellsDown = 5
    CellsAcross = 5
    
    '   Redimension temporary array
    ReDim MapArray(1 To CellsDown, 1 To CellsAcross)
     
    '   Set worksheet range
    Set TheRange = Range(Cells(1, 1), Cells(CellsDown, CellsAcross))
    
    For AR = 1 To CellsDown
        For AC = 1 To CellsAcross
            Randomize
            MapArray(AR, AC) = (Int((32 - 1 + 1) * Rnd + 1))
        Next AC
    Next AR
    
    With Range("A1")
        .CurrentRegion.ClearContents
        .Resize(CellsDown, CellsDown) = MapArray
    End With
    
    'TheRange.Value = MapArray
    'Worksheets("MAP").Range("A1:CV100").Value = MapArray
    
    With Application
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
    End Sub
    runtimeerr.png

  7. #7
    Registered User
    Join Date
    02-05-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    365
    Posts
    87

    Re: Tutorial for populating a 100x100 array with a random number between 1 and X

    Oh crap... I think i've figured it, though I don't understand why...

    I changed "Dim MapArray() As Byte" to Integer.... the code now works.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Tutorial for populating a 100x100 array with a random number between 1 and X

    You're welcome and thanks for rep+.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Random numbers but each number not eqully random.
    By richhhh in forum Excel General
    Replies: 11
    Last Post: 09-06-2016, 01:16 PM
  2. Replies: 5
    Last Post: 06-21-2013, 10:40 AM
  3. [SOLVED] Generate random 6-16-byte random hexadecimal number
    By jsamuelshn in forum Excel General
    Replies: 6
    Last Post: 07-20-2012, 10:38 AM
  4. A Good Array Formula Tutorial Anywhere?
    By agentred in forum Excel General
    Replies: 3
    Last Post: 11-02-2009, 02:56 AM
  5. Create a random array contain a fixed number
    By nguyennb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-13-2009, 05:38 AM
  6. array formula tutorial
    By Stefi in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-04-2006, 10:35 AM
  7. How do I find random number in list of random alpha? (Position is.
    By jlahealth-partners in forum Excel General
    Replies: 0
    Last Post: 02-08-2005, 02:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1