简体   繁体   中英

Insert copied cells from one sheet to another sheet

I want to copy all rows that have a specific value in column E and then insert them (NOT PASTE! so i want to insert new rows start at cell A29) on another sheet.

The sheet I want to copy from is called "owssvr" and the one I want to copy to is called "AOB Approval Form". I want to insert the copied rows starting Cell A29 in the "AOB Approval Form".

When i run the code, nothing happens. No error message pops up.

Few definition of my code below: LastRow: The last row of the "owssvr" sheet

PrimaryAOB: value that i want to lookup for in column 5. It is on the "AOB Approval Form" sheet

Here is my code:

For k = 2 To lastRow
    If Worksheets("owssvr").Range("E" & k).Value = primaryAOB Then
    Worksheets("owssvr").Rows(k).Copy
    Worksheets("AOB Approval Form").Rows(k + 27).Insert Shift:=xlDown
    Application.CutCopyMode = False
    End If
Next k

THANK YOU!

I copied your code into a new module in a blank workbook, then made the necessary mods to make it run (which it did). It looks the same as yours:

Sub Question()
Dim k As long, lastRow As Long
Dim primaryAOB As String

lastRow = Sheets(1).Range("E" & (ActiveSheet.Rows.Count)).End(xlUp).Row
primaryAOB = Sheets(2).Range("A1").Text
For k = 2 To lastRow
    If Worksheets("owssvr").Range("E" & k).Value = primaryAOB Then
    Worksheets("owssvr").Rows(k).Copy
    Worksheets("AOB Approval Form").Rows(k + 27).Insert Shift:=xlDown
    Application.CutCopyMode = False
    End If
Next k
End Sub

Since this worked, you may have just had some little syntax error somewhere while defining the variables. show us more of your procedure, that may reveal the issue! Also, have you run your code line by line? (F8)

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