Vba 根据单元格值向工作表中添加行

Vba 根据单元格值向工作表中添加行,vba,excel,Vba,Excel,我已经在谷歌上搜索并尝试了各种不同的方法来实现这一点,但仍然找不到解决方案 我仍在努力学习宏和VB,所以任何帮助都将不胜感激 Rows("8:" & (sheets("agreementterms").range("c8").Value + 8)).select Selection.insert shift:=xldown 基本上,我所追求的是一个工作表上的单元格值,即第二个工作表上的行数 如图所示,源单元格的值(付款数量)根据协议的期限/频率/值而变化。 然后,我希望在下一个

我已经在谷歌上搜索并尝试了各种不同的方法来实现这一点,但仍然找不到解决方案

我仍在努力学习宏和VB,所以任何帮助都将不胜感激

 Rows("8:" & (sheets("agreementterms").range("c8").Value + 8)).select
 Selection.insert shift:=xldown
基本上,我所追求的是一个工作表上的单元格值,即第二个工作表上的行数

如图所示,源单元格的值(付款数量)根据协议的期限/频率/值而变化。 然后,我希望在下一个工作表中分配行数,并按顺序编号

这就是我到目前为止一直在摸索的东西

    Sub ExtendByValue()  
'
' ExtendByValue Macro  
' Extends the rows by the number of repayments  
'

'
    Sheets("Agreement Tems").Select  
    Range("C8").Select  
    Selection.Copy  
    Sheets("Payments").Select  
    Range("M1").Select  
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _  
        :=False, Transpose:=False  
    Rows("8:8").Select  
    Application.CutCopyMode = False  
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove  
End Sub
在此方面的任何帮助都将不胜感激

 Rows("8:" & (sheets("agreementterms").range("c8").Value + 8)).select
 Selection.insert shift:=xldown
如果您有评论中指定的价值问题,请使用以下选项

 Rows("8:" & (sheets("agreementterms").range("c8").text + 8)).select
 Selection.insert shift:=xldown
试试这个

Option Explicit

Sub ExtendByValue()
'
' ExtendByValue Macro
' Extends the rows by the number of repayments
'
    Dim nRows As Long

    nRows = CLng(Sheets("Agreement Tems").Range("C8")) 'get the integer part of cell "C8" value in "Agreement Tems" sheet
    With Sheets("Payments")
        .Range("B8").Resize(nRows - 1).EntireRow.Insert 'insert nRows-1 below cell "B8" of "Payments" sheet, so as to have nRows with this latter included
        With .Range("B8").Resize(nRows) 'of these nRows ...
            .FormulaR1C1 = "=row()- row(R7)" ' ... fill column "B" with a formula returning integer form 1 to nRows ...
            .Value = .Value ' ... and finally get rid of formulas and leave only values
        End With
    End With

End Sub

您想在“付款”中的第8行之前插入156行吗?请阅读本网站关于在代码中不使用Select的内容。然后围绕最后一行(带有
.Insert
)构建一个循环,以获得所需的行数。谢谢。这是可行的,但我确实遇到了一些不匹配错误的问题。C8中的结果是将年、月和周转换为支付频率的四舍五入-因此,即使显示的结果是整数,单元格的真实值也有小数点,因此Excel无法理解如何将.7行输入。我已经解决了这个问题,只需将结果物理地键入一个单独的单元格,并将其用作引用。使用.text而不是.value。如果你只显示整数,这应该work@jollytas如果它解决了你的问题,考虑接受答案。下面是如何返回此处并对勾号/复选标记执行相同操作,直到其变为绿色。这会通知社区,找到了解决方案。否则,其他人可能会认为问题仍然悬而未决,并可能希望发布(更多)答案。