Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

? asked in Computers & InternetSoftware · 7 years ago

How do I have excel choose a random result from one column if the previous column is a number higher than 1660?

How do I have excel display a random result from one column (say column G) if the previous column is a number higher than 1660 and choose a random selection from a different column (say column H) if the number is lower than 1660. Is this possible?

1 Answer

Relevance
  • 7 years ago
    Favorite Answer

    I infer that you mean if column F 'has' a number greater than 1660, return a random number from column G, and if column F 'has' a number less than 1660, return a random number from column H.

    You do not state what you want to do with that number. Return it in a specific cell? Display a message box containing the number? Or... ?

    Additionally, you do not state how you wish to initiate the random number generation. Whenever a value is entered/changed in column F? Whenever a cell in column F is selected?

    Thus, the following example will display a message box with the appropriate random number from either column G or H, upon double clicking any cell in the worksheet.

    Copy the following event handler to the clipboard (highlight the entire code, right click inside the highlighted area, and 'Copy'):

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _

    Cancel As Boolean)

    Dim LastRowG, LastRowH, maxRnd, minRnd

    LastRowG = Range("G" & Rows.Count).End(xlUp).Row

    LastRowH = Range("H" & Rows.Count).End(xlUp).Row

    If Application.Max(Range("F:F")) > 1660 Then

    maxRnd = Val(Application.RoundUp(Rnd() * LastRowG, 0))

    MsgBox Range("G" & maxRnd).Value, vbOKOnly, "More than 1660"

    ElseIf Application.Max(Range("F:F")) < 1660 Then

    minRnd = Val(Application.RoundUp(Rnd() * LastRowH, 0))

    MsgBox Range("H" & minRnd).Value, vbOKOnly, "Less than 1660"

    End If

    Target.Offset(1).Select

    End Sub

    Select the worksheet containing the data you wish to evaluate and right click the sheet tab at the bottom.

    Select 'View Code'.

    Paste the event handler into the white editing area to the right (right click inside the area and 'Paste').

    Close the VBE (red button - top right).

    Double click any cell.

    Note: if the highest value in column F should happen to be 1660, no message will be displayed.

Still have questions? Get your answers by asking now.