【问题标题】:Autofill formula, but stop short自动填充公式,但停止
【发布时间】:2015-03-12 10:31:16
【问题描述】:

我有一个宏,可以在电子表格中插入一些行,这些行数由用户输入对话框来规定。我想要实现的是根据用户插入的行数将公式自动填充到相应的列中。

我目前的代码是:

Dim iInputRows As Integer
Dim iCount
iInputRows = CInt(InputBox("How many data entry rows required?")) 'message box for user input (interger)

If iInputRows > 1 Then
For iCount = 1 To iInputRows - 1
    Rows(iCount + 13 & ":" & iCount + 13).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 'insert no. of rows from from first row (row 13) to user input minus 1 (this ensures that the exact no. of rows are inserted from row 13 down)
    Range("D" & iCount + 13).Value = iCount + 1 'column D is used for sequential numbering purposes
Next iCount
End If

自动填充的公式是= X13:AR13

我对自动填充公式比较满意,但我在此应用程序中难以根据规定的行数停止自动填充。

【问题讨论】:

    标签: vba excel excel-formula autofill


    【解决方案1】:

    我不确定您对其余数据做了什么,但这应该可以:

    Sub aaa()
    Dim iInputRows As Integer
    Dim iCount
    iInputRows = CInt(InputBox("How many data entry rows required?"))
    
    If iInputRows > 1 Then
    For iCount = 1 To iInputRows - 1
        Rows(iCount + 13 & ":" & iCount + 13).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("D" & iCount + 13).Value = iCount + 1
    
    Next iCount
    Range("X13:AR13").AutoFill Destination:=Range("AX13:AR" & iInputRows + 13)
    End If
    End Sub
    

    【讨论】:

      【解决方案2】:

      您应该能够一次插入所有行,然后在 D 列中使用数据系列并在 X:AR 列中填写公式。

      Dim iInputRows As Long
      
      iInputRows = CInt(InputBox("How many data entry rows required?"))
      
      If iInputRows > 1 Then
          Rows(14).Resize(iInputRows, Columns.Count).EntireRow.Insert _
            Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
          Range("D13").Resize(iInputRows + 1, 1).DataSeries Rowcol:=xlColumns, _
            Type:=xlLinear, Step:=1
          Range("X13:AR13").Resize(iInputRows + 1, 21).FillDown
      End If
      

      【讨论】:

      • 谢谢吉普德。代码自动填充,但我得到的比用户规定的多一行,即 10 行输入 = 11 行插入。我不得不将参数“(iInputRows + 1,x)”更改为“(iInputRows,x)”,以便对其进行排序。
      猜你喜欢
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多