Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88087

Copy and paste rows based on keyword plus total row following VBA

$
0
0

I am fairly new to VBA and I am trying to write a macro that copies a row based on if a cell meets a criteria. I believe I found a macro that can accomplish that portion, the 2nd part is where I am struggling to find a solution.

If you can refer to the image I have provided, my macro is to look into column 3 of the first spreadsheet, "Original" and copy & paste the row into a new spreadsheet, "Apples", when it sees Apples.

What I am trying to also include is the Count total row that proceeds the copied and pasted row to also copy the row and paste into spreadsheet "Apples"

Spreadsheet Original

enter image description here

Spreadsheet Apples

enter image description here

Desired Spreadsheet Apples

enter image description here

Macro I am using to get my current results.

Sub Macro1()

a = Worksheets("Original").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To a

If Worksheets("Original").Cells(i, 3).Value = "Apples" Then

Worksheets("Original").Rows(i).Copy
Worksheets("Apples").Activate
b = Worksheets("Apples").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Apples").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Original").Activate
End If
Next
Application.CutCopyMode = False

ThisWorkbook.Worksheets("Original").Cells(1, 1).Select

End Sub

Viewing all articles
Browse latest Browse all 88087

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>