Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:28:23+00:00 2026-05-24T06:28:23+00:00

What is the simplest formula I can use to randomly choose a value in

  • 0

What is the simplest formula I can use to randomly choose a value in column A that is associated with a given B value. So in the table below, I’m looking to randomly choose an A where B = 3. So I’m randomly choosing between row 1 (5.4) and row 3 (4.2). Note that this table can be arbitrarily large.

    A     B

1   5.4   3          
2   2.3   1
3   4.2   3
4   9.2   2
    ...   ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:28:24+00:00Added an answer on May 24, 2026 at 6:28 am

    Conceptually you could do it a number of ways, but here’s one (VBA) where you’d use an array of possible choices then get a random element from that list:

    1. Create a udf that takes a range and the search value
    2. Loop through the row and if it equals your search value, get the value in the cell offset -1 and store it in an array
    3. Once you are done, you’ll have an array of all possible answers. Use the randbetween function and give it the lbound and ubound of your array.
    4. Return the i element where i is the random number it picked.

    UPDATE:
    Here is a code example that loops through the range for the number you specify, and if it find it, it adds the A column value to an array of possible results. Then a random number is generated and used to return a random value from that list.

    Function GetRand(ByVal cell_range As Range, ByVal criteria As Double) As Double
    
    Dim cell As Range
    Dim rNum As Long
    Dim i As Long
    Dim possibleChoices() As Double
    ReDim possibleChoices(1 To cell_range.Count)
    
    i = 1
    For Each cell In cell_range
        If cell.Value = criteria Then
            possibleChoices(i) = cell.Offset(0, -1).Value
            i = i + 1
        End If
    Next
    
    rNum = Application.WorksheetFunction.RandBetween(1, i - 1)
    GetRand = possibleChoices(rNum)
    
    End Function
    

    Optimization:
    Here is a more flexible version of the same function. It takes 3 paramteres – the range you want to look in, what you want to find, and the offset value of the cell you want a random result from. It also uses Variants, so you can search for text or numbers. So in your case, you’d write:

    =GetRand(B1:B5, 3, -1)
    

    Here is the code:

    Function GetRand(ByVal cell_range As Range, _
                     ByVal criteria As Variant, _
                     ByVal col_offset As Long) As Variant
    
    Application.ScreenUpdating = False
    Dim cell As Range
    Dim rNum As Long
    Dim i As Long
    Dim possibleChoices() As Variant
    ReDim possibleChoices(1 To cell_range.Count)
    
    i = 1
    For Each cell In cell_range
        If cell.Value = criteria Then
            possibleChoices(i) = cell.offset(0, col_offset).Value
            i = i + 1
        End If
    Next
    
    rNum = Application.WorksheetFunction.RandBetween(1, i - 1)
    
    GetRand = possibleChoices(rNum)
    Application.ScreenUpdating = True
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which are the approach (simplest possible) that we can use to get notified for
The simplest example that I can provide for what I want to do is
What's the simplest SQL statement that will return the duplicate values for a given
I have an array formula that outputs a single value, and I want to
I have the simplest of tasks that I can't figure out how to do
Can someone post the simplest thread example for Delphi 2010 that for example puts
I have the simplest table in the world, for looking up lat/lng values for
What would be the simplest ruby code to sort this table so that on
What is the simplest SQL query to find the second largest integer value in
What's the simplest-to-use techonlogy available to save an arbitrary Java object graph as an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.