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

时间:2016-04-22 04:33:06

标签: excel vba excel-vba

我用Google搜索并尝试了各种不同的方法来使其工作,但仍无法找到解决方案。

我仍在尝试学习宏和VB,所以任何帮助都会受到赞赏。

基本上,我所追求的是一个工作表上的单元格值是第二个工作表上的行数。

Picture showing what I am after

enter image description here

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

到目前为止,这是我设法摸索的......

    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

对此有任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

 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

答案 1 :(得分:0)

试试这个

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
相关问题