grunschlange
New Member
- Joined
- Sep 12, 2009
- Messages
- 13
Hello.
I was given assistance in coming up with the following code, which randomly selects a row from filtered data in one worksheet and pastes it in a second one:
Dim rngCol As Range
Dim lngCells As Long
Dim lngRandom As Long
Dim cel As Range
Dim i As Long
With Sheets("Office 1 Cases")
If .FilterMode Then
With .AutoFilter.Range
Set rngCol = .Columns(1) _
.Offset(1, 0) _
.Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
End With
Else
MsgBox "Filters not set." & vbLf & _
"Processing terminated."
Exit Sub
End If
End With
lngCells = rngCol.Cells.Count
Randomize
lngRandom = Int((lngCells * Rnd) + 1)
i = 0
For Each cel In rngCol
i = i + 1
If i = lngRandom Then
Range(cel, cel.Offset(0, 12)).Copy _
Destination:=Sheets("Office 1") _
.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0)
Exit For
End If
Next cel
How can this be modified to randomly select two or more rows of data from the worksheet and paste them in another?
Thanks.
I was given assistance in coming up with the following code, which randomly selects a row from filtered data in one worksheet and pastes it in a second one:
Dim rngCol As Range
Dim lngCells As Long
Dim lngRandom As Long
Dim cel As Range
Dim i As Long
With Sheets("Office 1 Cases")
If .FilterMode Then
With .AutoFilter.Range
Set rngCol = .Columns(1) _
.Offset(1, 0) _
.Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
End With
Else
MsgBox "Filters not set." & vbLf & _
"Processing terminated."
Exit Sub
End If
End With
lngCells = rngCol.Cells.Count
Randomize
lngRandom = Int((lngCells * Rnd) + 1)
i = 0
For Each cel In rngCol
i = i + 1
If i = lngRandom Then
Range(cel, cel.Offset(0, 12)).Copy _
Destination:=Sheets("Office 1") _
.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0)
Exit For
End If
Next cel
How can this be modified to randomly select two or more rows of data from the worksheet and paste them in another?
Thanks.