插入附加要求的行

我已经编译了一个使用input框的macros,允许用户在同一工作簿的三张不同的工作表的多个位置插入指定数量的行。 然后随后使用基于示例第一行的自动填充的适当数据填充这些新行。

它正确地完成了以上所有的事情。 但是,它也将用户指定的行数插入到运行macros时总是活动页的“Front Sheet”中。 这不是我指定插入行的目的地之一。

从用户testing中,不需要的行不是插入在一个一致的位置,而是出现在表单中的不同位置,看起来随input框中指定的数量而变化。 它不会填充这些多余的行,因为它是所需的。

Sub AddPlots() Dim j As Integer, r As Range, 'Set Number of Rows to be added j = InputBox("How many open market units does the development have?") ' Add Rows On Master Appraisal With Worksheets("Master Appraisal") Set r = Range("FirstPlot") Do Range(r.Offset(1, 0), r.Offset(j - 1, 0)).EntireRow.Insert Set r = Cells(r.Row + j + 1, 1) If r.Offset(1, 0) = "" Then Exit Do Loop End With 'Add Rows On Cashflow With Worksheets("Cashflow") Set r = Range("FirstPlot2") Do Range(r.Offset(1, 0), r.Offset(j - 1, 0)).EntireRow.Insert Set r = Cells(r.Row + j + 1, 1) If r.Offset(1, 0) = "" Then Exit Do Loop End With 'Add Rows On Fees (NHBC section) With Worksheets("Fees etc") Set r = Range("FirstPlot3") Do Range(r.Offset(1, 0), r.Offset(j - 1, 0)).EntireRow.Insert Set r = Cells(r.Row + j + 1, 1) If r.Offset(1, 0) = "" Then Exit Do Loop End With 'Add Rows On Fees (Marketing section) With Worksheets("Fees etc") Set r = Range("FirstPlot4") Do Range(r.Offset(1, 0), r.Offset(j - 1, 0)).EntireRow.Insert Set r = Cells(r.Row + j + 1, 1) If r.Offset(1, 0) = "" Then Exit Do Loop End With 'Populate New Rows with Data Worksheets("Cashflow").Range("Topline2").AutoFill Destination:=Range("Topline2").Resize(j), Type:=xlFillDefault Worksheets("Master Appraisal").Range("Topline").AutoFill Destination:=Range("Topline").Resize(j), Type:=xlFillDefault Worksheets("Fees etc").Range("Topline3").AutoFill Destination:=Range("Topline3").Resize(j), Type:=xlFillDefault Worksheets("Fees etc").Range("Topline4").AutoFill Destination:=Range("Topline4").Resize(j), Type:=xlFillDefault End Sub 

当您使用WITH....END WITH代码块时,要引用正确工作表的任何范围必须以句点( . )开头,否则将引用活动工作表。

所以Range(r.Offset(1, 0), r.Offset(j - 1, 0)).EntireRow.Insert引用活动表单,而.Range(r.Offset(1, 0), r.Offset(j - 1, 0)).EntireRow.Insert引用你的WITH语句中WITH表。

编辑:
此外, Cells(r.Row + j + 1, 1)应该是.Cells(r.Row + j + 1, 1) – 这些行将更新您在指定范围FirstPlot使用的工作表的r引用,以查看活动而不是WITH表。