+ Reply to Thread
Results 1 to 3 of 3

Running counter each time a function is executed (VBA)

Hybrid View

  1. #1
    Registered User
    Join Date
    04-23-2017
    Location
    Boulder, Colorado
    MS-Off Ver
    Excel
    Posts
    2

    Running counter each time a function is executed (VBA)

    I am struggling slightly with my indexing of my array. I want the upper bound of the array to be the amount of time's the Function RandomizeDice() executes. Any help is greatly appreciated.

    Function RandomizeDice()
        RandomizeDice = Application.WorksheetFunction.RandBetween(1, 6)
    End Function
    
    Sub RollDice()
        Dim DiceOne() As Variant
        Dim DiceTwo() As Variant
        Dim SumDice() As Variant
        Dim i As Integer
        ReDim DiceOne(i) As Variant
        ReDim DiceTwo(i) As Variant
        ReDim SumDice(i) As Variant
        Call arraySet(DiceOne(), DiceTwo(), SumDice())
        Debug.Print SumDice(i)
        'Debug.Print SumDice(0)
       ' Debug.Print ("Dice: " & DiceOne(0) & " " & DiceTwo(0))
       ' Debug.Print ("Sum: " & DiceOne(0) + DiceTwo(0))
    End Sub
    Sub arraySet(ByRef a() As Variant, b() As Variant, c() As Variant)
        'Dim DiceOne() As Integer
        'Dim DiceTwo() As Integer
        Dim i, j As Integer
        'Dim intSumDice() As Integer
        For i = 0 To j = i + 1
            a(i) = RandomizeDice() 'dice1
            b(i) = RandomizeDice() 'dice2
            c(i) = a(i) + b(i) 'sum
        Next i
        Debug.Print i
        Debug.Print ("Dice: " & a(0) & " " & b(0))
        Debug.Print ("Sum: " & a(0) + b(0))
    End Sub

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,117

    Re: Running counter each time a function is executed (VBA)

    As far as I can see, the index i is declared in both subroutines and i is not incremented in the RollDice routine. If you want both routines to use i, then either make it public or pass it as a parameter.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Running counter each time a function is executed (VBA)

    Not sure what your objective is.

    This will roll the dice pair 10 times and put the results in three arrays.

    Function RandomizeDice()
        Static bRandomized As Boolean
        If Not bRandomized Then Randomize: bRandomized = True 'Seed the randomizer once
        RandomizeDice = Int(6 * Rnd + 1)
    End Function
    
    Sub RollDice()
        Dim DiceOne() As Long
        Dim DiceTwo() As Long
        Dim SumDice() As Long
        Dim NumberOfRolls As Integer, i As Long
        
        NumberOfRolls = 10
        
        ReDim DiceOne(1 To NumberOfRolls)
        ReDim DiceTwo(1 To NumberOfRolls)
        ReDim SumDice(1 To NumberOfRolls)
        
        For i = 1 To NumberOfRolls
            DiceOne(i) = RandomizeDice
            DiceTwo(i) = RandomizeDice
            SumDice(i) = DiceOne(i) + DiceTwo(i)
            Debug.Print DiceOne(i) & " + " & DiceTwo(i) & " = " & SumDice(i)
        Next i
        
    End Sub
    Last edited by AlphaFrog; 04-23-2017 at 03:36 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ 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. Replies: 0
    Last Post: 10-05-2016, 10:26 AM
  2. [Contribution]Time counter/Clock
    By joejiang2001 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-07-2015, 04:50 PM
  3. time/counter format
    By Micbrook in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-19-2015, 06:08 PM
  4. Resetting running counter based on conditions being satisfied
    By Stdnt in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-17-2014, 06:15 PM
  5. Time Counter!
    By zks in forum Excel General
    Replies: 2
    Last Post: 01-20-2012, 01:33 PM
  6. Running counter using time
    By cdrum84 in forum Excel General
    Replies: 1
    Last Post: 06-06-2010, 11:47 AM
  7. Running counter
    By GeorgeBob in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-13-2009, 11:20 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