+ Reply to Thread
Results 1 to 5 of 5

Select random text value from column and assign to variable

  1. #1
    Forum Contributor
    Join Date
    02-24-2010
    Location
    BC, Canada
    MS-Off Ver
    Excel 2010
    Posts
    174

    Talking Select random text value from column and assign to variable

    I have a column with a bunch of text in column A in sheet "Rate".

    I want my macro to randomly select one of the text values and use that as the value for a variable.

    For example here is pseudo code:

    DIM y
    y = find value of random cell in column A in sheet Rate.


    So the column could have a list of fruit, and at the end of running this part of the code the value of y could be "Apples" by random selection

    Thanks

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Select random text value from column and assign to variable

    I don't know how many "fruits" are in column A but perhaps,

    Cells(WorksheetFunction.RandBetween(1, 24), 1).Select

  3. #3
    Forum Contributor
    Join Date
    02-24-2010
    Location
    BC, Canada
    MS-Off Ver
    Excel 2010
    Posts
    174

    Re: Select random text value from column and assign to variable

    Quote Originally Posted by JieJenn View Post
    I don't know how many "fruits" are in column A but perhaps,

    Cells(WorksheetFunction.RandBetween(1, 24), 1).Select
    This puts me on the right track. I failed to mention in my original post that "Rate" is not the active worksheet. Can a reference to that sheet be incorporated into this somehow? Thanks.

  4. #4
    Forum Contributor
    Join Date
    02-24-2010
    Location
    BC, Canada
    MS-Off Ver
    Excel 2010
    Posts
    174

    Re: Select random text value from column and assign to variable

    I tried this:

    y = Sheets("Rate").Cells(WorksheetFunction.RandBetween(3, 9), 1).Select

    And got this message:

    Run-time error '1004':
    Application-defined or object defined error


    Can you please help me adjust my code?

  5. #5
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Select random text value from column and assign to variable

    Change select to value

    y = Sheets("Rate").Cells(WorksheetFunction.RandBetween(3, 9), 1).Value

+ 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. Assign Variable Value Returned By SELECT Statement
    By scrumbag in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-24-2010, 03:18 PM
  2. Assign current cell to a variable and re-select
    By Cumberland in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-29-2006, 10:47 PM
  3. Replies: 3
    Last Post: 03-29-2006, 02:45 PM
  4. How do I assign a text value to a variable number of rows?
    By Jday in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM
  5. [SOLVED] How to assign a variable in a range select
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-03-2005, 07:05 PM

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