简体   繁体   English

根据Cell值插入行并填充

[英]Insert Rows based on a Cell value and fill down

I currently have a sheet with values that look like this, as an example: 我目前有一张表格看起来像这样的表格,作为一个例子:

1 A     B     C     D..............
2 1     Title of item 1
3       Formulas and formatting 1
4 2     Title of item 2
5       Formulas and formatting 2
6 3     Title of item 3
7       Formulas and formatting 3

What i want to happen is that the code looks up column A. If column A contains a number > 1 then it inserts that number (-1) rows, but 2 rows down. 我想要发生的是代码查找列A.如果列A包含一个> 1的数字,那么它将插入该数字(-1)行,但是向下插入2行。 I then need it fill the formulas (the formulas need to be dragged down) and formats down from the row above to the last row inserted for that section. 然后我需要填充公式(需要拖动公式)并从上面的行向下格式化为该部分插入的最后一行。 So it would look something like this: 所以它看起来像这样:

1 A     B     C     D...............
2 1     Title of item 1
3       Formulas and formatting 1
4 2     Title of item 2
5       Formulas and formatting 2
6       Formulas and formatting 2
7 3     Title of item 3
8       Formulas and formatting 3
9       Formulas and formatting 3
10      Formulas and formatting 3

And so on and so.... Note, it needs to drag the entire row formulas and foramts, not just AD... 等等......注意,它需要拖动整个行公式和foramts,而不仅仅是AD ...

I think I am almost there with the following code, but I can't get it to fill down from the first row with formulas, under the value in A, until the last row inserted for that section.... 我想我差不多有以下代码,但是我不能让它从第一行填充公式,在A中的值,直到为该部分插入的最后一行....

Here's my code: 这是我的代码:

Sub Add_Rows()
  Dim r As Long

  Application.ScreenUpdating = False
  For r = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
    If Cells(r, "A").Value > 1 Then Rows(r + 2).Resize(Cells(r, "A").Value - 1).Insert

  Next r
  Application.ScreenUpdating = True
End Sub

If any one could help me with the above that would be amazing!! 如果任何人可以帮助我上面的那将是惊人的! Equally, I think my method might be a bit clumsy, so I am open to more eloquent solutions too!! 同样,我认为我的方法可能有点笨拙,所以我也愿意接受更有说服力的解决方案! Thanks Guys, this forum has saved my skin so many times!!! 谢谢大家,这个论坛已经多次保存了我的皮肤! One day I hope I will get to a point where I can maybe answer some questions instead of always asking them! 有一天,我希望我能够回答一些问题,而不是总是问他们!

Try this. 尝试这个。 You're not actually copying and pasting anything. 你实际上并没有复制和粘贴任何东西。

Sub Add_Rows()

  Dim r As Long

  Application.ScreenUpdating = False

  For r = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
    If IsNumeric(Cells(r, "A")) Then
        If Cells(r, "A").Value > 1 Then
            Rows(r + 2).Resize(Cells(r, "A").Value - 1).Insert shift:=xlDown
            Rows(r + 1).Copy
            Rows(r + 2).Resize(Cells(r, "A").Value - 1).PasteSpecial xlPasteAll
        End If
    End If
  Next r

  Application.ScreenUpdating = True
  Application.CutCopyMode = False
  Application.Goto Range("A1")

End Sub

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

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