+ Reply to Thread
Results 1 to 4 of 4

filter randomly based on inputs

Hybrid View

aravindhan_31 filter randomly based on... 08-02-2008, 12:53 PM
mikerickson This routine will randomly... 08-02-2008, 06:39 PM
aravindhan_31 wow... 08-03-2008, 07:49 AM
mikerickson The header in Audit!A1 needs... 08-04-2008, 09:22 AM
  1. #1
    Forum Contributor
    Join Date
    03-28-2008
    Location
    India, bangalore
    MS-Off Ver
    Excel 2003,2007
    Posts
    216

    filter randomly based on inputs

    Hi,

    I have a master sheet with more than 5000 rows. sheet name is "Data". I have list of names in Column N.(eg John, Jim, etc)

    I need to filter randomly and paste the data in a new sheet based on the input.

    say i will have new sheet (Audit) with 2 columns Name and number of rows.

    If I enter as John in Column A and 20 in Column B in Audit Sheet, and run the macro, A filter function has to done in Data sheet based the values in Audit sheet. that is, Data sheet has to be filter by John, but only 20 rows has to filtered randomly and paste the same in a new sheet called John. ( no of rows for John may have 100 rows but I need only 20 rows randomly selected and pasted in sheet.

    similarly, If I enter as Jim in Column A and 40 in Column B in Audit Sheet and run the macro, Data sheet has to be filter by Jim, but only 40 rows has to filtered randomly and paste the same in a new sheet called Jim

    I know macro gods are here in this forum. would appreciate your help.

    I have posted the same question in http://www.mrexcel.com/forum/showthread.php?t=333016

    Arvind

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    This routine will randomly select rows from the filtered list. See attached spreadsheet for an example.
    Sub test()
        Dim dataRange As Range, extraColumn As Range
        Dim destRange As Range
        Dim critRange As Range
        Dim sampleSize As Long
        
        Rem set-up variables
        With ThisWorkbook.Sheets("Data")
            With Range(.Cells(1, .Columns.Count).End(xlToLeft), .Cells(.Rows.Count, 1).End(xlUp))
                Set extraColumn = .Offset(0, .Columns.Count).Resize(, 1)
                extraColumn.EntireColumn.Insert
                Set extraColumn = .Offset(0, .Columns.Count).Resize(, 1)
                Set dataRange = Application.Union(extraColumn, .Cells)
            End With
        End With
        With ThisWorkbook.Sheets("Audit")
            Set critRange = .Range("A1:A2")
            Set destRange = .Range("A4").Resize(1, dataRange.Columns.Count)
            destRange.Resize(.Rows.Count - 4, dataRange.Columns.Count).ClearContents
        End With
        sampleSize = Val(CStr(critRange.Offset(1, 1).Range("A1").Value))
        With extraColumn
            .FormulaR1C1 = "=ROW()"
            .Value = .Value
            .Cells(1, 1).Value = "From Data! row number"
        End With
        
        Rem filtered data to Audit sheet
        dataRange.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=critRange, _
            CopyToRange:=destRange, Unique:=False
        extraColumn.EntireColumn.Delete shift:=xlToLeft
        
        Rem select Random rows
        With destRange
            With Range(.Cells(2, .Columns.Count), .Parent.Cells(Parent.Rows.Count, 1).End(xlUp))
                If sampleSize < .Rows.Count Then
                    .Offset(0, .Columns.Count).Resize(, 1).FormulaR1C1() = "=RAND()"
                
                    With .Resize(, .Columns.Count + 1)
                        .Sort Key1:=.Cells(1, .Columns.Count), Order1:=xlAscending, Header:=xlGuess, _
                            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
                    End With
                    .Offset(0, .Columns.Count).Resize(, 1).ClearContents
                    .Offset(sampleSize, 0).ClearContents
                    With .Resize(sampleSize, .Columns.Count)
                        .Sort Key1:=.Cells(1, .Columns.Count), Order1:=xlAscending, Header:=xlGuess, _
                            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
                    End With
                End If
            End With
        End With
        destRange.Parent.Activate
        Application.Goto destRange.Range("A1")
    End Sub
    Attached Files Attached Files
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Contributor
    Join Date
    03-28-2008
    Location
    India, bangalore
    MS-Off Ver
    Excel 2003,2007
    Posts
    216

    wow...

    Hi,

    Thanks a lot its working.. this is what i wanted.
    But When I put this code in my original data, I am not getting the result.
    You have given the criteria as 1st column in Data, that "Name" Column.

    Now the column which I need to give critria is L column, How do i change the code.

    I have attached the excel sheet with original data.

    thanks for your help.
    Attached Files Attached Files

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    The header in Audit!A1 needs to match the header in Data!L4.
    This line needs to be changed to reflect your headers being in row 4 of Data.
      Rem set-up variables
        With ThisWorkbook.Sheets("Data")
            With Range(.Cells(4, .Columns.Count).End(xlToLeft), .Cells(.Rows.Count, 1).End(xlUp))
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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