+ Reply to Thread
Results 1 to 13 of 13

Need a macro to do a random 10% Audit

Hybrid View

  1. #1
    Registered User
    Join Date
    12-12-2013
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010
    Posts
    92

    Need a macro to do a random 10% Audit

    Hello,

    I need assistance in creating a macro that will randomly select 10% of information on Sheet 1(Log) and copy the information to Sheet 4 (Audit). The information on Sheet 1 starts in row 9 due to information for data validation being housed in lines 1-8. I tried searching online for different macros and wasn't successful in finding one that will work with minor tweaks.

    Thank you for your assistance.

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Need a macro to do a random 10% Audit

    Sub ciresuark()
    
    Set ws1 = Sheets("Log")
    Set ws2 = Sheets("Audit")
    OpenRow = 9
    LastRow = ws1.Cells(Rows.Count, "A").End(xlUp).Row
    CRows = Round((LastRow - 8) * 0.1)
    
    For i = 1 To CRows
        Do
            CRow = CInt(Rnd() * LastRow)
        Loop Until CRow > 8
        ws1.Rows(CRow).EntireRow.Copy ws2.Rows(OpenRow)
        OpenRow = OpenRow + 1
    Next
    
    End Sub

  3. #3
    Registered User
    Join Date
    12-12-2013
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: Need a macro to do a random 10% Audit

    That is perfect! Thank you so much for your help!

  4. #4
    Registered User
    Join Date
    12-12-2013
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: Need a macro to do a random 10% Audit

    What was previously supplied is awesome. Is it possible to add to it an option to do a 10% audit for a name in cell A1, then do a 10% audit for the name in cell A2? The results of the would be in order on the following page. Thoughts?

  5. #5
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Need a macro to do a random 10% Audit

    10% each of the total number of names, or 10% each for each name?

    If example if you had 100 Lisas and 200 Bobs, would you want:
    1. 30 Lisas and 30 Bobs, or
    2. 10 Lisas and 20 Bobs

  6. #6
    Registered User
    Join Date
    12-12-2013
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: Need a macro to do a random 10% Audit

    10 Lisas and 20 Bobs. So 10% for each name.

  7. #7
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Need a macro to do a random 10% Audit

    Got it. See code below.

    Note: This assumes that the names for each row are also listed in column A. I don't think you specified where they are in each row.

    Sub ciresuark2()
    Set ws1 = Sheets("Log")
    Set ws2 = Sheets("Audit")
    
    OpenRow = 9
    LastRow = ws1.Cells(Rows.Count, "A").End(xlUp).Row
    
    With ws1.Range("A9:A" & LastRow)
    Set c = .Find(ws1.Range("A1").Value, lookat:=xlWhole)
    If Not c Is Nothing Then
        FirstAdd = c.Address
        Do
        n1 = n1 + 1
        Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAdd
    End If
    
    Set c = .Find(ws1.Range("A2").Value, lookat:=xlWhole)
    If Not c Is Nothing Then
        FirstAdd = c.Address
        Do
        n2 = n2 + 1
        Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAdd
    End If
    
    End With
        
    
    CRows1 = Round((n1) * 0.1)
    CRows2 = Round((n2) * 0.1)
    
    i = 0
    Do
        Do
            CRow = CInt(Rnd() * LastRow)
        Loop Until CRow > 8
        If ws1.Range("A" & CRow).Value = ws1.Range("A1").Value Then
            ws1.Rows(CRow).EntireRow.Copy ws2.Rows(OpenRow)
            OpenRow = OpenRow + 1
            i = i + 1
        End If
    Loop Until i = CRows1
    
    i = 0
    Do
        Do
            CRow = CInt(Rnd() * LastRow)
        Loop Until CRow > 8
        If ws1.Range("A" & CRow).Value = ws1.Range("A2").Value Then
            ws1.Rows(CRow).EntireRow.Copy ws2.Rows(OpenRow)
            OpenRow = OpenRow + 1
            i = i + 1
        End If
    Loop Until i = CRows2
    
    End Sub

  8. #8
    Registered User
    Join Date
    12-12-2013
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: Need a macro to do a random 10% Audit

    Sorry. I thought about that before I went to a meeting. The names for the audit are in row G.

  9. #9
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Need a macro to do a random 10% Audit

    Adjusted accordingly.

    Sub ciresuark2()
    Set ws1 = Sheets("Log")
    Set ws2 = Sheets("Audit")
    
    OpenRow = 9
    LastRow = ws1.Cells(Rows.Count, "G").End(xlUp).Row
    
    With ws1.Range("G9:G" & LastRow)
    Set c = .Find(ws1.Range("A1").Value, lookat:=xlWhole)
    If Not c Is Nothing Then
        FirstAdd = c.Address
        Do
        n1 = n1 + 1
        Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAdd
    End If
    
    Set c = .Find(ws1.Range("A2").Value, lookat:=xlWhole)
    If Not c Is Nothing Then
        FirstAdd = c.Address
        Do
        n2 = n2 + 1
        Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAdd
    End If
    
    End With
        
    
    CRows1 = Round((n1) * 0.1)
    CRows2 = Round((n2) * 0.1)
    
    i = 0
    Do
        Do
            CRow = CInt(Rnd() * LastRow)
        Loop Until CRow > 8
        If ws1.Range("G" & CRow).Value = ws1.Range("A1").Value Then
            ws1.Rows(CRow).EntireRow.Copy ws2.Rows(OpenRow)
            OpenRow = OpenRow + 1
            i = i + 1
        End If
    Loop Until i = CRows1
    
    i = 0
    Do
        Do
            CRow = CInt(Rnd() * LastRow)
        Loop Until CRow > 8
        If ws1.Range("G" & CRow).Value = ws1.Range("A2").Value Then
            ws1.Rows(CRow).EntireRow.Copy ws2.Rows(OpenRow)
            OpenRow = OpenRow + 1
            i = i + 1
        End If
    Loop Until i = CRows2
    
    End Sub

  10. #10
    Registered User
    Join Date
    12-12-2013
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: Need a macro to do a random 10% Audit

    Thank you for your help. It works great. One scenario I am thinking of is if a person only has 9 entries it won't pull any data to audit until they have 10. Would it be easy to code to have it pull a minimum of 1 if the number is less than 10%?

  11. #11
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Need a macro to do a random 10% Audit

    Sure, Change your CRows1 = and CRows2 = lines to:

    CRows1 = Application.WorksheetFunction.RoundUp(n1 * 0.1, 0)
    CRows2 = Application.WorksheetFunction.RoundUp(n2 * 0.1, 0)

  12. #12
    Registered User
    Join Date
    12-12-2013
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: Need a macro to do a random 10% Audit

    Awesome. That worked. Thank you again for your help!

  13. #13
    Registered User
    Join Date
    08-31-2015
    Location
    Pakistan
    MS-Off Ver
    2016
    Posts
    2

    Re: Need a macro to do a random 10% Audit

    what would be the code if selection criteria is based on 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. Audit Log Problems
    By SpiritedAway in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-06-2013, 11:52 AM
  2. Save audit log
    By Bags84 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2013, 03:28 PM
  3. Macro to copy rows by date for a teacher audit
    By Bpd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-30-2013, 11:08 AM
  4. Audit report
    By mlk in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-05-2007, 07:31 PM
  5. Random pick (multi column) info for audit
    By sjmgeezer in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-12-2007, 05:57 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