+ Reply to Thread
Results 1 to 2 of 2

VBA code to select random values from a column and insert them in another sheet

  1. #1
    Registered User
    Join Date
    08-29-2013
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2007
    Posts
    7

    VBA code to select random values from a column and insert them in another sheet

    Hi all,
    Can anyoane assist with the VBA code to select random values from a column and paste them on another sheet in the same workbook?
    Here is what I have:
    Sheet1.ColumnB where I have to paste data
    Sheet2.ColumnA from where I have to select randomly cells (text values) and insert them in Sheet1 without repeating same selected value.

    Thank you!!!

  2. #2
    Registered User
    Join Date
    08-29-2013
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: VBA code to select random values from a column and insert them in another sheet

    I have found the code for it. I'm displating it here in case someone else needs it:

    Sub Random()
    Dim CountCells
    Dim RandCount
    Dim LastRow
    Dim Counter1
    Dim Counter2
    Worksheets("Sheet1").Select
    Range("A1").Select
    CountCells = Worksheets("Sheet1").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count 'quantity of random numbers to pick from
    If CountCells = 0 Then Exit Sub
    On Error Resume Next
    Application.DisplayAlerts = False
    RandCount = Application.InputBox(Prompt:="How many random numbers do you want?", _
    Title:="Random Numbers Selection", Type:=1)
    On Error GoTo 0
    Application.DisplayAlerts = True
    RandCount = Int(RandCount)
    If Int(RandCount) <= 0 Or RandCount = False Then Exit Sub
    If RandCount > CountCells Then
    MsgBox "Requested quantity of numbers is greater than quantity of available data"
    Exit Sub
    End If
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    'clear working area
    Range("B:C").ClearContents
    'clear destination area
    Range("Sheet2!A:A").ClearContents
    'create index for sort use
    Range("B1") = 1
    Range(Cells(1, 2), Cells(LastRow, 2)).DataSeries , Step:=1
    'create random numbers for sort
    Range("C1") = "=RAND()"
    Range("C1").Copy Range(Cells(1, 3), Cells(LastRow, 3))
    'randomly sort data
    Range(Cells(1, 1), Cells(LastRow, 3)).Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    'data has been sorted randomly, cells in column A, rows 1 through the quantity desired will be chosen
    Counter1 = 1
    Counter2 = 1
    Do Until Counter1 > RandCount
    Range("Sheet2!A" & Counter1) = Cells(Counter2, 1).Value
    Counter1 = Counter1 + 1
    Counter2 = Counter2 + 1
    Loop
    'resort data into original order and clear working area
    Range(Cells(1, 1), Cells(LastRow, 3)).Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    Range("B:C").ClearContents
    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. [SOLVED] how to set select case values from sheet 2 vba code
    By sspreyer in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 08-29-2013, 12:27 PM
  2. Select Random Rows and Display on new sheet
    By matt4003 in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 02-27-2013, 06:54 AM
  3. Select a random block of range by column
    By anyse in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-29-2011, 12:27 PM
  4. Select data in Sheet 2 based on column values in Sheet 1
    By Snehith in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-22-2011, 08:52 PM
  5. Working Macro Code to insert random value. Only C48 needs to go to G15 etc.
    By Phixtit in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-26-2009, 06:56 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