[Solved] Cut specified number of rows in selected range VBA


The following will achieve what you are wanting, it will generate 18 random numbers between 2 and your last row with data, in your case row 180 and then copy that row into the next free row in Sheet2:

Sub foo()
Dim wsOriginal As Worksheet: Set wsOriginal = ThisWorkbook.Worksheets("Sheet1")
Dim wsDestination As Worksheet: Set wsDestination = ThisWorkbook.Worksheets("Sheet2")
'declare and set the worksheets you are working with, amend as required
Dim i As Long, LastRowOrig As Long, LastRowDest As Long

LastRowOrig = wsOriginal.Cells(wsOriginal.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A on your Sheet with data

For i = 1 To 18 'loop 18 times
    RandNumber = Int((LastRowOrig - 2 + 1) * Rnd + 2)
    'generate a random number between 2 and 180 (Last Row)
    LastRowDest = wsDestination.Cells(wsDestination.Rows.Count, "A").End(xlUp).Row + 1
    'get the last row with data on Destination sheet and offset by one (i.e. next free row)
    wsOriginal.Rows(RandNumber).Copy 'copy the row
    wsDestination.Rows(LastRowDest).PasteSpecial xlPasteAll 'paste the row
Next i
End Sub

UPDATE:

To reflect your comment and add a new workbook with the random rows in it, use the following code:

Sub foo()
Dim wsOriginal As Worksheet: Set wsOriginal = ThisWorkbook.Worksheets("Sheet1")
Dim wsDestination As Worksheet
Dim i As Long, LastRowOrig As Long, LastRowDest As Long

Set NewWorkbook = Workbooks.Add 'create a new workbook
    With NewWorkbook
        .Title = "Random Rows" 'You can modify this value.
        .SaveAs Filename:="C:\Users\doneby\Desktop\RandomGeneratedRows.xlsx"
        'amend the line above to the path you and name of the file you want to create
    End With
Set wsDestination = NewWorkbook.Worksheets("Sheet1") 'specify the Sheet of the new workbook
'declare and set the worksheets you are working with, amend as required

LastRowOrig = wsOriginal.Cells(wsOriginal.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A on your Sheet with data

For i = 1 To 18 'loop 18 times
    RandNumber = Int((LastRowOrig - 2 + 1) * Rnd + 2)
    'generate a random number between 2 and 180 (Last Row)
    LastRowDest = wsDestination.Cells(wsDestination.Rows.Count, "A").End(xlUp).Row + 1
    'get the last row with data on Destination sheet and offset by one (i.e. next free row)
    wsOriginal.Rows(RandNumber).Copy 'copy the row
    wsDestination.Rows(LastRowDest).PasteSpecial xlPasteAll 'paste the row
Next i
NewWorkbook.Close SaveChanges:=True
'close and save the new workbook
End Sub

4

solved Cut specified number of rows in selected range VBA