+ Reply to Thread
Results 1 to 5 of 5

Randomize VBA Code

  1. #1
    Registered User
    Join Date
    05-23-2020
    Location
    Southampton, England
    MS-Off Ver
    Office 365
    Posts
    6

    Randomize VBA Code

    Hi

    Below is a VBA code I have come across that selects records at random. This does everything I need it to do for the project I have been tasked with, however this code highlights the records that have been selected. How do I change it so that it no longer highlights.

    Private Sub cmdPickRandom_Click()

    Dim lLastRow As Long

    wksHome.AutoFilterMode = False

    If Trim(txtRandomCount.Text) = "" Or IsNumeric(txtRandomCount.Text) = False Then
    MsgBox "All data showing", vbInformation
    Exit Sub
    End If

    If txtRandomCount.Text <= 0 Then
    MsgBox "Invalid sample numbers to be picked", vbInformation
    Exit Sub
    End If

    If CInt(txtRandomCount.Text) <> txtRandomCount.Text Then
    MsgBox "Invalid sample numbers to be picked", vbInformation
    Exit Sub
    End If

    lLastRow = wksHome.Range("C" & Rows.Count).End(xlUp).Row

    If lLastRow < 19 Then
    MsgBox "It seems there is no data in the sheet" & vbNewLine & vbNewLine & "Note: Column C of the Home sheet should not have empty cells", vbInformation
    Exit Sub
    End If

    If txtRandomCount.Text > lLastRow - 18 Then
    MsgBox "Number of records to be picked cannot be greater than total records available in the sheet.", vbInformation
    Exit Sub
    End If

    Application.ScreenUpdating = False

    'Clear old formulas
    wksHome.Range("A:B").ClearContents

    'Clear formatting
    With wksHome.Range("19:" & Rows.Count).Interior
    .Pattern = xlNone
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With

    'Add SN
    wksHome.Range("A19").Value = "=A18+1"

    'Copy formula
    wksHome.Range("A19").Copy wksHome.Range("A19:A" & lLastRow)

    'Random formula
    wksHome.Range("B19").Value = "=RANDBETWEEN(1," & lLastRow - 18 & ")"

    'Copy formula
    wksHome.Range("B19").Copy wksHome.Range("B19:B" & lLastRow)

    'Calculate Formula
    wksHome.Calculate

    'Convert to value
    wksHome.Range("A19:B" & lLastRow).Value = wksHome.Range("A19:B" & lLastRow).Value

    'Sort the data by Random number
    wksHome.Sort.SortFields.Clear
    wksHome.Sort.SortFields.Add Key:=wksHome.Range("B19:B" & lLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With wksHome.Sort
    .SetRange wksHome.Range(wksHome.Cells(18, 1), wksHome.Cells(lLastRow, wksHome.Cells.SpecialCells(xlCellTypeLastCell).Column))
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    'Color sample records
    wksHome.Range("19:" & CInt(txtRandomCount.Text) + 18).Interior.Color = vbGreen

    'Sort the data by SN
    wksHome.Sort.SortFields.Clear
    wksHome.Sort.SortFields.Add Key:=wksHome.Range("A19:A" & lLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With wksHome.Sort
    .SetRange wksHome.Range(wksHome.Cells(18, 1), wksHome.Cells(lLastRow, wksHome.Columns.Count))
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    'Clear formulas
    wksHome.Range("A:B").ClearContents

    wksHome.AutoFilterMode = False
    wksHome.Range("18:" & lLastRow).AutoFilter
    wksHome.Range(wksHome.Cells(18, 1), wksHome.Cells(lLastRow, wksHome.Columns.Count)).AutoFilter Field:=3, Criteria1:=RGB(0, 255, 0), Operator:=xlFilterCellColor

    Application.ScreenUpdating = True

    MsgBox "Done", vbInformation

    Unload Me

    End Sub


    I am currently using Office 365

    Many thanks

    Evie

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,336

    Re: Randomize VBA Code

    If you don't highlight, how do you know what has been picked?


    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional.)
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    05-23-2020
    Location
    Southampton, England
    MS-Off Ver
    Office 365
    Posts
    6

    Re: Randomize VBA Code

    It filters down the list once you have selected the amount of records you require then brings the records upto the top

  4. #4
    Registered User
    Join Date
    05-23-2020
    Location
    Southampton, England
    MS-Off Ver
    Office 365
    Posts
    6

    Re: Randomize VBA Code

    I have attached a sample workbook so that you know what I am getting at
    Attached Files Attached Files

  5. #5
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Randomize VBA Code

    As soon as you comply with post #2, somebody may be able to help.
    HTH
    Regards, Jeff

+ 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. VBA code to randomize with conditions
    By halimgunawan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-26-2020, 02:34 PM
  2. Code to randomize within a filter
    By ScottBeatty in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-30-2013, 04:32 PM
  3. Randomize
    By Valkyrie1981 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-22-2010, 11:27 AM
  4. Randomize
    By shaun0_0 in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 09-12-2008, 02:24 PM
  5. Randomize
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-19-2008, 06:46 AM
  6. randomize data
    By rmg2828 in forum Excel General
    Replies: 1
    Last Post: 07-17-2006, 12:05 PM
  7. Randomize
    By Davor ®upaniæ in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-11-2005, 02:05 AM

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