anewman5high
New Member
- Joined
- Aug 25, 2017
- Messages
- 11
Hello,
I'm trying to get a piece of code to sample 10% of rows in a sheet by copying them into another sheet.
I've managed to do this but the caveat is that I only want rows which have data in Column A. The way I've tried to do this so far is by hiding anything without data in column A but the vba still copies the hidden ones over.
This is the code so far:
Bonus points if the solution can only select from rows where the data in column A starts with UR.
Even more bonus points if the code can copy the rows below the one picked at random until it finds another row with data in column A.
Thanks in advance, let me know if you need more info!
Alan
I'm trying to get a piece of code to sample 10% of rows in a sheet by copying them into another sheet.
I've managed to do this but the caveat is that I only want rows which have data in Column A. The way I've tried to do this so far is by hiding anything without data in column A but the vba still copies the hidden ones over.
This is the code so far:
Code:
Sub TakeSample()
Dim LastRow As Long
Dim NbRows As Long
Dim RowList()
Dim i As Long, J As Long, k As Long
Dim RowNb As Long
Dim s As String
Sheets("All Data").Activate
Application.ScreenUpdating = False
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
s = i & ":" & i
If IsEmpty(Cells(i, 1).Value) Then
Rows(s).EntireRow.Hidden = True
End If
Next
Application.ScreenUpdating = True
Sheets("All Data").Activate
LastRow = Range("A" & Rows.Count).End(xlUp).Row
NbRows = LastRow * 0.1
'NbRows = IIf(LastRow < 200, LastRow * 0.1, 10)
ReDim RowList(1 To NbRows)
k = 1
For i = 1 To NbRows
RowNb = Rnd() * LastRow
For J = 1 To k
If (RowList(J) = RowNb) Then GoTo NextStep
Next J
RowList(k) = RowNb
Rows(RowNb).Copy Destination:=Sheets("1 in 10 Sample").Cells(k, "A")
k = k + 1
NextStep:
Next i
End Sub
Bonus points if the solution can only select from rows where the data in column A starts with UR.
Even more bonus points if the code can copy the rows below the one picked at random until it finds another row with data in column A.
Thanks in advance, let me know if you need more info!
Alan