简体   繁体   English

将复制的单元格从一张纸插入另一张纸

[英]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. 我想复制在E列中具有特定值的所有行,然后将它们插入(不要粘贴!所以我要在单元格A29处插入新行)在另一张纸上。

The sheet I want to copy from is called "owssvr" and the one I want to copy to is called "AOB Approval Form". 我要复制的工作表称为“ owssvr”,我要复制的工作表称为“ AOB批准表”。 I want to insert the copied rows starting Cell A29 in the "AOB Approval Form". 我想在“ AOB批准表”中插入从单元格A29开始的复制行。

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 以下是我的代码的很少定义:LastRow:“ owssvr”工作表的最后一行

PrimaryAOB: value that i want to lookup for in column 5. It is on the "AOB Approval Form" sheet PrimaryAOB:我要在第5列中查找的值。它在“ AOB批准表”表上

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) (F8)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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