I'm trying to select random rows within my Active Worksheet filtered data, copy the rows, and have them pasted within another sheet in the workbook. I'm trying to use this code, however I keep getting an error on the Else statement for the TargetRows. Here's the code:
I'm unsure where the hiccup is. The code breaks at the TargetRows section of the code. Thank you in advance.
D.
VBA Code:
Sub Row_Selection()
Const STARTROW As Long = 1
Dim LastRow As Long
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
Dim RowArr() As Long
ReDim RowArr(STARTROW To LastRow)
Dim i As Long
For i = LBound(RowArr) To UBound(RowArr)
RowArr(i) = i
Next i
Randomize
Dim tmp As Long, RndNum As Long
For i = LBound(RowArr) To UBound(RowArr)
RndNum = WorksheetFunction.Floor((UBound(RowArr) - LBound(RowArr) + 1) * Rnd, 1) + LBound(RowArr)
tmp = RowArr(i)
RowArr(i) = RowArr(RndNum)
RowArr(RndNum) = tmp
Next i
Const LIMIT As Double = 0.1 '10%
Dim Size As Long
Size = WorksheetFunction.Ceiling((UBound(RowArr) - LBound(RowArr) + 1) * LIMIT, 1)
If Size > UBound(RowArr) Then Size = UBound(RowArr)
Dim TargetRows As Range
For i = LBound(RowArr) To LBound(RowArr) + Size
If TargetRows Is Nothing Then
Set TargetRows = ActiveSheet.Rows(RowArr(i))
Else
Set TargetRows = Union(TargetRows, ActiveSheet.Rows(RowArr(i)))
End If
Next i
Dim OutPutRange As Range
Set OutPutRange = Sheet1.Cells(1, 1) 'Top Left Corner
TargetRows.Copy Destination:=OutPutRange.Resize(TargetRows.Rows.Count).EntireRow
End Sub
I'm unsure where the hiccup is. The code breaks at the TargetRows section of the code. Thank you in advance.
D.