如何在Excel中多次复制和插入行或将行复制X次?
如果我们想手动复制Excel表格中现有的行,那将是一个耗时的过程,因为我们需要插入并复制值。我们可以使用VBA应用程序来自动化此过程。
阅读本教程,了解如何多次复制和插入一行,或在Excel中将一行复制“X”次。在这里,我们将首先插入VBA模块,然后运行代码以完成任务。让我们看一下在Excel中多次复制和插入一行或将一行复制“X”次的简单步骤。
步骤1
让我们考虑一个Excel工作表,其中包含如下所示的表格。
要打开VBA应用程序,请单击“插入”,选择“查看代码”,单击“插入”,选择“模块”,然后将下面提到的程序1键入文本框中,如下图所示。
程序1
Sub test()
'Update By Nirmal
Dim xCount As Integer
LableNumber:
xCount = Application.InputBox("Number of Rows", "Duplicate the rows", , , , , , 1)
If xCount < 1 Then
MsgBox "the entered number of rows is error, please enter again", vbInformation, "Select the values"
GoTo LableNumber
End If
ActiveCell.EntireRow.Copy
Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(xCount, 0)).EntireRow.Insert Shift:=xlDown
Application.CutCopyMode = False
End Sub
步骤2
现在将工作表另存为宏启用工作表,单击要复制的值,然后单击F5,选择要复制的次数,然后单击“确定”。
如果我们想复制整行,可以使用程序2代替程序1。
程序2
Sub insertrows()
'Update By Nirmal
Dim I As Long
Dim xCount As Integer
LableNumber:
xCount = Application.InputBox("Number of Rows", "Duplicate whole values", , , , , , 1)
If xCount < 1 Then
MsgBox "the entered number of rows is error ,please enter again", vbInformation, "Enter no of times"
GoTo LableNumber
End If
For I = Range("A" & Rows.CountLarge).End(xlUp).Row To 2 Step -1
Rows(I).Copy
Rows(I).Resize(xCount).Insert
Next
Application.CutCopyMode = False
End Sub
结论
在本教程中,我们使用了一个简单的示例来演示如何在Excel中多次复制和粘贴行或列。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP