daily pastebin goal


a guest Feb 22nd, 2014 29 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub copyRows()
  3. Dim i As Integer, j As Integer, k As Integer
  4. Dim bReport As Workbook, bReport2 As Workbook
  5. Dim Report As Worksheet, Report2 As Worksheet
  7. Set Report = Excel.ActiveSheet 'This assumes your sheet with data is the active sheet.
  8. Set bReport = Report.Parent
  9. Set bReport2 = Excel.Workbooks.Add 'This opens a new workbook
  10. Set Report2 = bReport2.Worksheets(1) 'This assigns the variable for the worksheet where we will be pasting our data.
  12. Report2.Cells(1, 1).Value = "Copied Rows" 'This gives a title to the worksheet (helps to avoid having to identify _
  13.                                             the initial iteration of our for...next loop
  15. For i = 1 To Report.UsedRange.Rows.Count 'Loops once for each row in the worksheet.
  17.     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.
  18.                                                                                     'You could also disable the binary comparison for the subroutine to accomplish this, but why bother?
  19.         Report.Cells(i, 1).EntireRow.Copy 'This copies the row
  20.         j = Report2.UsedRange.Rows.Count + 1 'This finds the row below the last used row in the new worksheet/workbook.
  21.         Report2.Cells(j, 1).EntireRow.Insert (xlDown) 'This pastes the copied rows.
  22.     End If
  23. Next i
  25. End Sub
RAW Paste Data