+ Reply to Thread
Results 1 to 9 of 9

Assistance with Audit code...

Hybrid View

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

    Assistance with Audit code...

    Hello,

    I found this code searching online. The concept works; however, I am having trouble tweaking it to my needs. The code takes a random 10% of the information on sheet 1. What I would like it to do is to take random information from sheet 1 starting with line 9 instead of the whole sheet and have it pasted on Sheet 2 in the second line. Any assistance would be appreceiated.

    Option Explicit
    Sub Random10()
    Randomize 'Initialize Random number seed
    Dim MyRows() As Integer    ' Declare dynamic array.
    Dim numRows, percRows, nxtRow, nxtRnd, chkRnd, copyRow As Integer
    'Determine Number of Rows in Sheet1 Column A
      numRows = Sheets(1).Range("B" & Rows.Count).End(xlUp).Row
    'Get 10% of that number
       percRows = numRows * 0.1
    'Allocate elements in Array
        ReDim MyRows(percRows)
    'Create Random numbers and fill array
         For nxtRow = 1 To percRows
    getNew:
    'Generate Random number
          nxtRnd = Int((numRows) * Rnd + 1)
    'Loop through array, checking for Duplicates
           For chkRnd = 1 To nxtRow
    'Get new number if Duplicate is found
            If MyRows(chkRnd) = nxtRnd Then GoTo getNew
           Next
    'Add element if Random number is unique
          MyRows(nxtRow) = nxtRnd
         Next
    'Loop through Array, copying rows to Sheet2
      For copyRow = 1 To percRows
       Sheets(1).Rows(MyRows(copyRow)).EntireRow.Copy _
         Destination:=Sheet2.Cells(copyRow, 1)
         
      Next
    End Sub

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Assistance with Audit code...

    Sub Random10()
    Randomize 'Initialize Random number seed
    Dim MyRows() As Integer    ' Declare dynamic array.
    Dim numRows, percRows, nxtRow, nxtRnd, chkRnd, copyRow As Integer
    'Determine Number of Rows in Sheet1 Column A
      numRows = Sheets(1).Range("B" & Rows.Count).End(xlUp).Row
    'Get 10% of that number
       percRows = numRows * 0.1
    'Allocate elements in Array
        ReDim MyRows(percRows)
    'Create Random numbers and fill array
         For nxtRow = 9 To percRows
    getNew:
    'Generate Random number
          nxtRnd = Int((numRows) * Rnd + 1)
    'Loop through array, checking for Duplicates
           For chkRnd = 1 To nxtRow
    'Get new number if Duplicate is found
            If MyRows(chkRnd) = nxtRnd Then GoTo getNew
           Next
    'Add element if Random number is unique
          MyRows(nxtRow) = nxtRnd
         Next
    'Loop through Array, copying rows to Sheet2
      For copyRow = 1 To percRows
       Sheets(1).Rows(MyRows(copyRow)).EntireRow.Copy _
         Destination:=Sheet2.Cells(copyRow + 1, 1)
         
      Next
    End Sub

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

    Re: Assistance with Audit code...

    Thank you for the reply. When I pasted the code I receive an "Object required" error message. Thoughts?

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Assistance with Audit code...

    In which line does the code show an error(stopped)?

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

    Re: Assistance with Audit code...

    I noticed I copied over the Option Explicit. I added that then the code stopped at
    Destination:=Sheet2.Cells(copyRow + 1, 1)
    . I looked at my sheets and I had Sheet 4. I updated it to Sheet4. Now I receive an error message stating "400". Thoughts?

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Assistance with Audit code...

    I have now tested the code. The error was not new. The error comes from this line
     ReDim MyRows(percRows)
    if MyRows is 0, the code errors.
    You need to split that line in to 2, to see the error

    Sheets(1).Rows(MyRows(copyRow)).Copy
       Sheet2.Cells(copyRow, 1).PasteSpecial xlValues
    since MyRows is 0, the code errors as it is an arrat

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

    Re: Assistance with Audit code...

    I'm sorry. I'm not understanding what I need to change to make this work on row 9 instead of row 1.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Assistance with Audit code...

    The code does not work. It is ridden with error, but if you think it is working then please show me with a sample so that I can amend it. You should attach code with the sample as I need to test and amend it.

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

    Re: Assistance with Audit code...

    Thank you for your assistance. I will post a new thread trying to get a macro created to randomly copy 10% from Sheet 1 to sheet 2. Is this something you are able to assist with?

+ 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] VBA Code Assistance
    By kapeller in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-20-2014, 03:52 AM
  2. VBA code for Audit schedule email-weekly
    By Hense in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-27-2012, 04:42 AM
  3. assistance requested to score audit compliance
    By sandysafe in forum Excel General
    Replies: 0
    Last Post: 05-01-2012, 06:08 PM
  4. stock audit & bar code scanner
    By joemahony in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-28-2008, 12:53 AM
  5. Code assistance
    By Jim May in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-24-2005, 01:05 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