+ Reply to Thread
Results 1 to 2 of 2

Adding a counter variable to code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-10-2013
    Location
    Wolverhampton
    MS-Off Ver
    Excel 2010
    Posts
    101

    Adding a counter variable to code

    Hi I have the following code (see below) which generates a random number between a lower and an upper limit. It then recalculates the worksheet 1000 times, each time adding the random number to a cumulative total, the only problem is I do not know how to add a counter, a counter is used to keep track of how many times the loop has repeated. The counter is a variable that I want to create especially for this purpose. Can someone please help me incorporate this into the code.

    Private Sub cmdCalculate_Click()
      
      Dim intCounter As Integer
      Dim intLower As Integer
      Dim intUpper As Integer
      Dim ingCumTotal As Long
      Dim ingRandom As Long
    
      [C6] = 0
      [F6] = 0
      
      intLower = [D6]
      intUpper = [E6]
       
      For i = 1 To 1000
        
        ingRandom = Int((intUpper - intLower + 1) * Rnd + intLower)
        
        ingCumTotal = [F6]
      
        [F6] = ingCumTotal + ingRandom
      Next i
        
    ''MsgBox "ingRandom=" & ingRandom
    
    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
    49,046

    Re: Adding a counter variable to code

    You already have a counter ... i ... you're using that to control the repeat of the loop. At any point in the iteration, i will tell you how many times you have gone through the loop.


    Regards, TMS
    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


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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