Hello,
I have the vba below that works fine to copy 10% to a new sheet for auditing various ranges of data. I am hoping to see if there is a way to tweak the code to make it even better though. What I am curious to see is how to:
I have the vba below that works fine to copy 10% to a new sheet for auditing various ranges of data. I am hoping to see if there is a way to tweak the code to make it even better though. What I am curious to see is how to:
- Speed up the vba. Though it runs fine, on 70k rows, it seems to hang. It's not a game killer but I suspect there is something I'm unaware of.
- I would like the sheet to be automatically renamed "Audit - " & whatever the source sheet name. When I update the Set Target to match the concatenate of the Add piece of code, I get a debug error so I'm obviously wrong with that.
- if at all possible, to loop through each worksheet rather than running each sheet one by one. This one is quite a stretch and I believe that if this were in place, #1 would remain slow.
VBA Code:
Sub AuditRange()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim Pct As Long
Dim CntRow As Long
Dim Rw As Long
Dim Source As Worksheet
Dim Target As Worksheet
Set Source = ActiveSheet
CntRow = Cells.Find("*", after:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Sheets.Add(after:=Sheets(Sheets.Count)).Name = "Audit"
Set Target = Sheets("Audit")
Source.Rows(1).Copy Target.Range("A1")
Pct = Int(0.1 * CntRow)
If Pct > 100 Then Pct = 100
Do While Pct > 0
Rw = WorksheetFunction.RandBetween(2, CntRow)
Source.Rows(Rw).Copy Target.Range("A" & Rows.Count).End(xlUp).Offset(1)
Pct = Pct - 1
Loop
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub