+ Reply to Thread
Results 1 to 3 of 3

make a random selection from 5 cells in a filtered sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-02-2012
    Location
    Austria
    MS-Off Ver
    Excel 2007
    Posts
    457

    make a random selection from 5 cells in a filtered sheet

    Hi all,

    I found some code to make a random selection of 5 cells that then I paste into a second sheet for further processing.
    the code does not seem to actually make a random selection, but maybe it is just me.
    First I use a filter to to have only cases status in column D with Complete. Then the random code should run and give me 5 random cells from column A.
    It actually uses all of the cells in a selection, but just ingores the filter and takes also cells that are not in the filtered ragne.

    I get the excel file from a csv export and the sheet has the date in the sheet name.

    Any chance the below code could be adjusted to work:

    Sub GetRandom()
        Dim iRows As Integer
        Dim iCols As Integer
        Dim iBegRow As Integer
        Dim iBegCol As Integer
        Dim J As Integer
        Dim sCells As String
        Dim LastRow As Long
        Set TempDO = New DataObject
        
        LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
        LastRow2 = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
    
        
          Rows("1:1").Select
            Selection.AutoFilter
                ActiveSheet.Range("A1:A" & LastRow).AutoFilter Field:=4, Criteria1:="Complete"
        Range("A2:A" & LastRow2).Select
        
        iRows = Selection.Rows.Count
        iCols = Selection.Columns.Count
        iBegRow = Selection.Row
        iBegCol = Selection.Column
        
        If iRows < 16 Or iCols > 1 Then
            MsgBox "Too few rows or too many columns"
        Else
            Randomize Timer
            sCells = ""
            For J = 1 To 5
                iWantRow = Int(Rnd() * iRows) + iBegRow
                sCells = sCells & Cells(iWantRow, iBegCol) & vbCrLf
            Next J
            TempDO.SetText sCells
            Sheets("Sheet1").Select
            TempDO.PutInClipboard
            Range("A1").PasteSpecial
    
        End If
    End Sub
    Greetings.

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: make a random selection from 5 cells in a filtered sheet

    I wouldnt bother filtering beforehand, do the random selection and then check if its a completed one, if not random again...loop until it is completed.

    I notice as well that your code doesnt take into account the fact it may already have chosen that row so you could end up with less than 5 results.
    If someone has helped you then please add to their Reputation

  3. #3
    Forum Contributor
    Join Date
    08-02-2012
    Location
    Austria
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: make a random selection from 5 cells in a filtered sheet

    hi,

    I fixed the code some how to filter the ones I don't need and then delete them. I need to do this twice (also for second criteria) and it works perfect this way.

    I still have problems with the random selection as it tends to select often 2 times the same number in the range selected.
    Any way this could be avoided? maybe it would have to check if the number is not in the selection and then if so, go for another round of random selection (this is over my head to come up with)

    this is the code I got so far:

    ' use this dll if  "Set TempDO = New DataObject" does not work: FM20.DLL
    Sub GetRandom2()
        Dim iRows As Integer
        Dim iCols As Integer
        Dim iBegRow As Integer
        Dim iBegCol As Integer
        Dim J As Integer
        Dim sCells As String
        Dim LastRow As Long
        Set TempDO = New DataObject
        
        LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
            
          Rows("1:1").Select
            Selection.AutoFilter
                ActiveSheet.Range("A1:A" & LastRow).AutoFilter Field:=4, Criteria1:="<>Complete"
                    
                            
               LastRow2 = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
                
                Range("A2:A" & LastRow2).EntireRow.Delete shift:=xlUp
                
                Selection.AutoFilter
                
                LastRow4 = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
                
                Selection.AutoFilter
                
                ActiveSheet.Range("A1:A" & LastRow4).AutoFilter Field:=3, Criteria1:="<>Awaiting 2nd check"
                
                
                 LastRow5 = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
                
                Range("A2:A" & LastRow5).EntireRow.Delete shift:=xlUp
                
    
    Selection.AutoFilter
                
                LastRow3 = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
                
        Range("A2:A" & LastRow3).Select
        
        iRows = Selection.Rows.Count
        iCols = Selection.Columns.Count
        iBegRow = Selection.Row
        iBegCol = Selection.Column
        
        If iRows < 16 Or iCols > 1 Then
            MsgBox "Too few rows or too many columns"
        Else
            Randomize Timer
            sCells = ""
            For J = 1 To 5
                iWantRow = Int(Rnd() * iRows) + iBegRow
                sCells = sCells & Cells(iWantRow, iBegCol) & vbCrLf
            Next J
            TempDO.SetText sCells
                    Worksheets.Add().Name = "Selection"
            
            Sheets("Selection").Select
            TempDO.PutInClipboard
            Range("A1").PasteSpecial
    
        End If
    End Sub
    Greetings.

+ 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. Replies: 1
    Last Post: 12-23-2015, 02:08 PM
  2. [SOLVED] Sheet 1 button click resulting in random sheet 2 selection
    By Summers1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2014, 05:31 AM
  3. Replies: 5
    Last Post: 06-22-2012, 05:28 AM
  4. Replies: 2
    Last Post: 05-15-2012, 02:08 AM
  5. Random Selection of Row Numbers from a Filtered Range
    By Saarang84 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-14-2010, 09:07 AM
  6. Error when no cells are filtered for selection
    By josnah in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-06-2008, 01:24 PM
  7. random selection from a range of cells
    By tjb in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-15-2005, 06:45 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