使用Inputbox在多个工作表中插入X行数

时间:2016-11-25 04:37:46

标签: excel-vba vba excel

几乎让它发挥作用。得到异常的结果。很难找到信息让我到达终点。什么工作...... 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

这不是全部,但我希望它足以说明这一点。

1 个答案:

答案 0 :(得分:0)

我无法复制使用您的代码提到的结果,而是我插入了正确的行数但仅在第一张表中。现在看来,当您使用最后一行来确定它时,您想要插入的行下面没有数据。如果是这种情况,您可以使用copy而不是insert。

我提出了两种方法:

  1. 要一次插入或复制(两个选项都已注释,请取消注释您要使用的选项)所有必需的行。此方法主要取决于Select
  2. 的使用
  3. 要插入或复制(两个选项都已注释,请取消注释您要使用的选项)工作表中所有必需的行。我更喜欢这种方法,因为它不使用Select
  4. 第一种方法

    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