将单元格插入由条件指定的工作表和行

时间:2013-07-26 11:53:13

标签: excel vba excel-vba

我正在拼命寻找VBA代码的帮助。我的问题非常详细和特别,因此我决定将其作为代码中的注释。如果这样的行为使我的问题模糊或模糊,请告诉我。 简而言之,我的代码包含一行(来自“FSR”工作簿),该行满足标准并将其粘贴到特定工作表和特定行(由“FSR”工作表中的值“P”和“Q”列给出)。我需要更多的是插入单元格(并将现有单元格向下移动)到我的代码粘贴复制单元格的相同位置,但在它粘贴它们之前。我需要将单元格插入到A:J列中,并插入到工作表(“FSR”)中“G”列中的值给出的行中。我需要将这个新代码放入FOR循环中,因为这个插入以及复制和粘贴需要循环。 现在编写评论中的问题代码:

Sub FromSOF()
Dim Worksheet As Worksheets
startrow = Worksheets("GUTS").Cells(10, 1)
endrow = Worksheets("GUTS").Cells(11, 1)

For x = startrow To endrow
    If Cells(x, "O").Value = "P" Then
        Worksheets(Cells(x, "P").Value).Select 'And here is the porblem. This one and three consecutive lines are to insert cells and shift the existing cells down. The cells need to be inserted into the worksheet given by the value in column "P" in Worksheet("FSR") and into the row given by the column "Q" in Worksheet("FSR")
        Range("A" & Worksheet("FSR").Cells(x, "P").Value, "J" & Worksheet("FSR").Cells(x, "P").Value).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Worksheets("FSR").Select
        Range("E" & x, "H" & x).Copy
        Worksheets(Cells(x, "P").Value).Cells(Cells(x, "Q").Value, "C").PasteSpecial xlPasteAll
    End If
    Next
End Sub

提前谢谢大家! 我希望我对代码的斗争将因你的礼貌而结束 最诚挚的问候 Artur Rutkowski

1 个答案:

答案 0 :(得分:0)

下面的代码将为其创建并复制数据:

'Create line
Worksheets(Worksheets("FSR").Cells(x,"P").Value).Rows(Worksheets("FSR").Cells(x,"Q").Value).EntireRow.Insert
'Copy data (without clipboard)
Range("E" & x, "H" & x).Copy Worksheets(Worksheets("FSR").Cells(x,"P").Value).Cells(Worksheets("FSR").Cells(x,"Q").Value,1)