- Sub copyRows()
- Dim i As Integer, j As Integer, k As Integer
- Dim bReport As Workbook, bReport2 As Workbook
- Dim Report As Worksheet, Report2 As Worksheet
- Set Report = Excel.ActiveSheet 'This assumes your sheet with data is the active sheet.
- Set bReport = Report.Parent
- Set bReport2 = Excel.Workbooks.Add 'This opens a new workbook
- Set Report2 = bReport2.Worksheets(1) 'This assigns the variable for the worksheet where we will be pasting our data.
- Report2.Cells(1, 1).Value = "Copied Rows" 'This gives a title to the worksheet (helps to avoid having to identify _
- the initial iteration of our for...next loop
- For i = 1 To Report.UsedRange.Rows.Count 'Loops once for each row in the worksheet.
- If InStr(1, Left(Report.Cells(i, 2).Value, 1), "e", vbTextCompare) = 1 Then 'This searches the first letter of the value. I used the instr function to make it case-insensitive.
- 'You could also disable the binary comparison for the subroutine to accomplish this, but why bother?
- Report.Cells(i, 1).EntireRow.Copy 'This copies the row
- j = Report2.UsedRange.Rows.Count + 1 'This finds the row below the last used row in the new worksheet/workbook.
- Report2.Cells(j, 1).EntireRow.Insert (xlDown) 'This pastes the copied rows.
- End If
- Next i
- End Sub
a guest Feb 22nd, 2014 29 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
RAW Paste Data