自动填充公式,但请不要太短

我有一个macros插入到电子表格行数,由用户input到对话框中规定的行数。 我想要实现的是根据用户插入的行数自动填充相应列的公式。

我现在的代码是:

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

我对自动填充公式感到相当舒服,但是在这个应用程序中,我很难按照规定的行数来停止自动填充。

您应该能够一次插入所有行,然后使用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 

我不知道你在做什么与其余的数据,但这应该工作:

 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