我尝试编写VBA来复制行,并将其插入可变次数。我附上了一个样本工作簿。
VBA说明: 基本上,VBA必须在表格中列出部门中的FTE数量,然后在表格#34; Manning Input"中复制第6行。并在第6行下方插入该行数 - 1。 在示例中,我希望VBA复制第6行,并将其插入27次(部门240中有28名员工,28-1 = 27),在第6行下面。然后公式应该完成。
我目前有以下代码。它有效,但我怀疑有更快的方法来完成它。无论如何,这是迄今为止的代码:
Sub PasteRows()
Application.ScreenUpdating = False
' Defines variables
Dim Rowcount As String, x As Long
Sheets("Manning input").Range("E6").Select
' Update variable RowCount with the value entered in an input box
Rowcount = Range("B3").Value
Range("6:6").Copy
' If the RowCount value is a valid number then...
If IsNumeric(Rowcount) Then
' Loop for RowCount number of times
For x = 1 To Rowcount
' Insert a row above the active row
ActiveCell.Offset(1, 0).EntireRow.Insert Shift:=xlDown
' Loop for RowCount number of times
Next x
' Else is the value of RowCount is not a number then...
Else
' Display an error
MsgBox "The value entered is not a valid number. Please try again", vbOKOnly, "Warning!"
End If
Application.CutCopyMode = False
Call copypaste
Application.ScreenUpdating = True
End Sub
我已经在这里找到了缓慢的部分:
Sub copypaste()
'
' Macro3 Macro
'
'
Columns("A:D").Select
Range("D1").Activate
Selection.EntireColumn.Hidden = False
Range("A6:H6").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Columns("A:C").Select
Range("C1").Activate
Selection.EntireColumn.Hidden = True
Range("D1").Select
Application.CutCopyMode = False
End Sub
感谢您的支持// Soren
编辑:更新了代码