几乎让它发挥作用。得到异常的结果。很难找到信息让我到达终点。什么工作...... InputBox问我要插入多少行。我可以选择多个工作表来添加行。我可以找到并选择表格中的最后一行。当我尝试复制行(带公式),并在工作表上粘贴所需数量的行时,一切都会出错。第2和第3张工作正常。第一张表添加行* 2(Exp:Add 10 ...结果为20行)。这就是我到目前为止所拥有的......
Dim iRows As Integer
Dim sRows As String
sRows = InputBox("How many rows do you want to add? (Between 1 - 100)")
iRows = Int(Val(sRows))
Sheets(Array("ETC LABOR LOE", "ETC LABOR HOURS", "ETC LABOR COST")).Select
Range("A9").Select
Selection.End(xlDown).Select
For i = 1 To iRows
ActiveCell.EntireRow.Select
Selection.Copy
Selection.Offset(1).Insert Shift:=xlDown
Next
这不是全部,但我希望它足以说明这一点。
答案 0 :(得分:0)
我无法复制使用您的代码提到的结果,而是我插入了正确的行数但仅在第一张表中。现在看来,当您使用最后一行来确定它时,您想要插入的行下面没有数据。如果是这种情况,您可以使用copy而不是insert。
我提出了两种方法:
Select
。Select
。第一种方法
Sub Insert_LastRow_AtOnce()
Dim bRows As Byte
Rem Ask input from user
bRows = Application.InputBox("How many rows do you want to add? (Between 1 - 100)", Type:=2)
With ThisWorkbook.Sheets("ETC LABOR LOE")
'NOTE: If you want to use Copy uncomment this block
Rem Copy
' Application.Goto .Cells(1), 1
' .Range("A9").End(xlDown).Offset(1).Resize(bRows).Select
' Sheets(Array("ETC LABOR LOE", "ETC LABOR HOURS", "ETC LABOR COST")).Select
' Range("A9").End(xlDown).EntireRow.Copy
' ActiveSheet.Paste
' Application.CutCopyMode = False
'NOTE: If you want to use Copy\Insert uncomment this block
Rem Copy\Insert
' Application.Goto .Cells(1), 1
' .Range("A9").End(xlDown).Offset(1).Resize(bRows).Select
' Sheets(Array("ETC LABOR LOE", "ETC LABOR HOURS", "ETC LABOR COST")).Select
' Range("A9").End(xlDown).EntireRow.Copy
' Selection.Insert Shift:=xlDown
' Application.CutCopyMode = False
End With
End Sub
第二种方法
Sub Insert_LastRow_ByWsh()
Dim wsh As Worksheet
Dim bRows As Byte
Rem Ask input from user
bRows = Application.InputBox("How many rows do you want to add? (Between 1 - 100)", Type:=2)
For Each wsh In ThisWorkbook.Sheets(Array("ETC LABOR LOE", "ETC LABOR HOURS", "ETC LABOR COST"))
With wsh
'NOTE: If you want to use Copy uncomment this block
Rem Copy
' .Range("A9").End(xlDown).EntireRow.Copy
' .Range("A9").End(xlDown).Offset(1).Resize(bRows).PasteSpecial
' Application.CutCopyMode = False
'NOTE: If you want to use Copy\Insert uncomment this block
Rem Copy\Insert
' .Range("A9").End(xlDown).EntireRow.Copy
' .Range("A9").End(xlDown).Offset(1).Resize(bRows).Insert Shift:=xlDown
' Application.CutCopyMode = False
End With: Next
End Sub