简体   繁体   中英

How to Copy and Paste Rows starting from a specific cell in another sheet

I want to copy all the rows from the Pipeline Report sheet that contain the text "AllScripts" in column T .

Then, I want to paste them onto another sheet starting from "A14" and go down.

Right now this code pastes them from "A2" but I need it to start from A14 and go down from there.

Sub extractAllscripts() 

Dim myrange As Range 
Dim lr as Long

Sheets("Pipeline Report").Select 
Set myrange = Sheets("Pipeline Report").Range("T1", Range("T" & Rows.Count).End(xlUp)) 

For Each cell In myrange 
    If cell.Value = "Allscripts" Then 
        lr = Sheets("Macro Test Page").Range("T" & Rows.Count).End(xlUp).Row
        cell.EntireRow.Copy Destination:=Sheets("Macro Test Page").Range("A" & lr + 1) 
    End If 
Next cell 

End Sub 

This is a bad practice but as we don't know how your sheet is set up, then I would suggest this:

Sub extractAllscripts() 

Dim myrange As Range 
Dim lr as Long
Dim myoffset As Integer

Sheets("Pipeline Report").Select 
Set myrange = Sheets("Pipeline Report").Range("T1", Range("T" & Rows.Count).End(xlUp)) 

myoffset = 13

For Each cell In myrange 
 If cell.Value = "Allscripts" Then 
     lr = Sheets("Macro Test Page").Range("T" & Rows.Count).End(xlUp).Row
     cell.EntireRow.Copy Destination:=Sheets("Macro Test Page").Range("A" & lr + myoffset) 
    End If
    myoffset = 1 
Next cell 

End Sub 

Or another bad solution:

In your code substitute lr = Sheets("Macro Test Page").Range("T" & Rows.Count).End(xlUp).Row with lr = lr + 1 and define lr to be 13 outside of the loop and in the Range("A" & lr + 1) use lr instead of lr + 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM