Copying Rows from 1 sheet to another based on a cell value

JADownie

Active Member
Joined
Dec 11, 2007
Messages
395
Hello, I am hoping that someone might be able to give me some guidance here today.

Would something like this even be possible now with VBA, or am I dreaming when I think that this could be done....

First, on my sheet Summary delete all rows active rows on this sheet after last row in PivotTable1

Next, the same sheet Summary determine the first available empty row (skip 3 more)

Then look in cell B1 on sheet Summary, and in this blank row from above below copy over all rows from sheet AllData where B1 = same value in Column C on AllData sheet (also copy over header row 1)
 
That worked and it pulled in the header row now, but it still pasted in the next row right after my last Pivot table. I was hoping it could skip 2 rows so that they would not be right on top of each other.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Oh I see, so when pasting the data, first look for the last used row, then skip the next two rows, and then paste, correct? If so, try the following instead...

VBA Code:
Sub CopyRows()

    Dim c As Range
    Dim LastRow As Long
    Dim NextRow As Long
    Dim Source As Worksheet
    Dim Target As Worksheet
    
    
    Set Source = ActiveWorkbook.Worksheets("AllData")
    With Source
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    Set Target = ActiveWorkbook.Worksheets("Summary")
    With Target
        NextRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 3
    End With
    
    Source.Rows(1).Copy Target.Rows(NextRow)
    
    NextRow = NextRow + 1
    
    For Each c In Source.Range("A1:A" & LastRow)
        If c.Value = Target.Range("B1").Value Then
            c.EntireRow.Copy Target.Rows(NextRow)
            NextRow = NextRow + 1
        End If
    Next c
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top