+ Reply to Thread
Results 1 to 6 of 6

Random formula for two variables

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2013
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2007
    Posts
    27

    Random formula for two variables

    I have a database that contains among everything else...a list of items numbers and a list of associate names aligned to those item numbers. I am trying to take a sample of 2 items that each associate produced. So in theory, if Sally produced 10 items (Items: 1001, 1203, 4503, 4542, 4555, 5555, 5612, 6564, 8832, 9222) and I wanted to randomly pull 2 of those items out based on just Sally's name (among all my list of associates), how can I formulate a formula to do so? Anyone? Thanks!


    Josh

  2. #2
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Random formula for two variables

    How are you getting those 10 items from Sally?

    One way to randomly choose something in a list would be

    =CHOOSE(randbetween(1,10),CHOICE1,CHOICE2,CHOICE3,.....CHOICE10)
    You should hit F5, because chances are I've edited this post at least 5 times.
    Example of Array Formulas
    Quote Originally Posted by Jacc View Post
    Sorry, your description makes no sense. I just made some formula that looks interesting cause I had nothing else to do.
    Click the * below on any post that helped you.

  3. #3
    Registered User
    Join Date
    04-21-2013
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: Random formula for two variables

    Those 10 are in my company's data source every morning.

  4. #4
    Registered User
    Join Date
    04-21-2013
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: Random formula for two variables

    The CHOOSE formula would only provide for a select number (2 in this case) for just Sally, right? But what if I needed to go each associate by each associate and there are 1000 associates that I need to pull 2 from?

  5. #5
    Registered User
    Join Date
    04-21-2013
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: Random formula for two variables - CHALLENGE

    So, if you look at the attachment. I need to randomly choose 2 Item Numbers for Janell Abraham, 2 for Angelina Acosta, 2 for Mark Adler...so on and so on...

    Any suggestions?

    Thanks!

    Josh
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,190

    Re: Random formula for two variables

    This assumes you have NAME, item1, item2, ...
    Alter as you wish.

    Sub RandomList()
    Dim vName
    Dim r As Integer, c As Integer, j As Integer
    Dim col As Collection
    Dim shtApp As Worksheet, shtOut As Worksheet
    Const kKEEP = 2
    
    Set shtApp = ActiveSheet
    Worksheets.Add
    Set shtOut = ActiveSheet
    shtApp.Activate
    
    Range("A2").Select
    While ActiveCell.Value <> ""
       vName = ActiveCell.Value
      shtOut.Activate
      ActiveCell.Value = vName
      shtApp.Activate
        
      c = 1
      Set col = New Collection
                            'get all values into collection
      While ActiveCell.Offset(0, c).Value <> ""
         col.Add ActiveCell.Offset(0, c).Value
         c = c + 1
      Wend
       
                        'sift thru collection for randoms
      shtOut.Activate
      j = 1
      If col.Count > kKEEP Then
          c = col.Count
          For i = 1 To kKEEP
             r = Int((c - 2 + 1) * Rnd() + 2)  'start at 2nd item,1st is name
                 'put 1 item random
             ActiveCell.Offset(0, j).Value = col(c)
             j = j + 1
                 
                'remove item
             col.Remove r
             c = c - 1
          Next
      Else
         For c = 1 To col.Count                                    'put all the items
             ActiveCell.Offset(0, c).Value = col(c)
         Next
      End If
      ActiveCell.Offset(1, 0).Select   'next row
      shtApp.Activate
     
       ActiveCell.Offset(1, 0).Select   'next row
    Wend
    
    Set col = Nothing
    Set shtApp = Nothing
    Set shtOut = Nothing
    End Sub

+ 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: 3
    Last Post: 09-30-2012, 03:03 PM
  2. Generating Poisson Random Variables
    By telk02 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2012, 04:56 PM
  3. [SOLVED] Copy/Insert based on random variables?
    By fourmurphys in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-11-2012, 03:12 PM
  4. Non-random numbers generated by excel's data analysis random gener
    By Allie in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 09-17-2005, 02:05 AM
  5. Replies: 2
    Last Post: 09-13-2005, 12:05 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