跳到主要内容

如何在Excel中多次复制和插入行或重复x次复制行?

在日常工作中,您是否曾经尝试过复制一行或每一行,然后在工作表中当前数据行的下方多次插入? 例如,我有一个单元格区域,现在,我想复制每一行并将它们粘贴3次到下一行,如下图所示。 您如何在Excel中处理这项工作?


使用VBA代码多次复制并插入特定行

如果您只想重复x特定的一行x,下面的VBA代码可能会帮助您,请按照下列步骤操作:

1。 指定要复制并插入多次的行,然后按住 ALT + F11 键,然后打开 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

VBA代码:多次复制一个特定行:

Sub test()
'Updateby Extendoffice
    Dim xCount As Integer
LableNumber:
    xCount = Application.InputBox("Number of Rows", "Kutools for Excel", , , , , , 1)
    If xCount < 1 Then
        MsgBox "the entered number of rows is error, please enter again", vbInformation, "Kutools for Excel"
        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

3。 粘贴代码后,请按 F5 键运行此代码,并弹出一个提示框,提醒您输入要重复的次数,请参见屏幕截图:

4。 然后点击 OK 按钮,并且在所选行下方插入了三个新的复制行,请参见屏幕截图:


轻松地根据特定数字多次复制并插入每一行

通常,除了手动处理复制和插入操作外,没有很好的方法可以多次复制和插入行。 但是,随着 Kutools for Excel's 根据单元格值复制行/列 功能,您可以轻松解决此问题。 点击下载Kutools for Excel!

Kutools for Excel:具有300多个方便的Excel加载项,可以在60天内免费试用,没有任何限制。 立即下载并免费试用!


使用VBA代码多次复制并插入每一行

要在一个范围内多次复制每一行,您可以应用以下VBA代码,请执行以下操作:

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

VBA代码:每行重复多次:

Sub insertrows()
'Updateby Extendoffice
Dim I As Long
Dim xCount As Integer
LableNumber:
xCount = Application.InputBox("Number of Rows", "Kutools for Excel", , , , , , 1)
If xCount < 1 Then
MsgBox "the entered number of rows is error ,please enter again", vbInformation, "Kutools for Excel"
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

3。 然后按 F5 键以运行此代码,并弹出一个提示框,提醒您输入要为每个记录重复的次数,请参见屏幕截图:

4。 然后点击 OK,并且每行已复制并插入到活动行下方3次,请参见屏幕截图:

备注:在上面的代码中, A 表示数据范围从A列开始(如果您的数据从A列开始) K, 请更换 AK 根据您的需要。


具有令人敬畏的功能,可根据特定数字多次复制并插入每一行

也许您不熟悉VBA代码,或者担心该代码会使您的数据崩溃。 在这里,我将介绍一个有用的功能, Kutools for Excel's 根据单元格值复制行/列,使用此实用程序,您可以根据指定的编号快速复制并插入行。

提示:申请这个 根据单元格值复制行/列 功能,首先,您应该下载 Kutools for Excel,然后快速轻松地应用该功能。

安装后 Kutools for Excel,请这样做:

1。 在数据旁边的单元格列表中输入要复制行的重复数字,请参见屏幕截图:

2。 点击 库工具 > 插页 > 根据单元格值复制行/列,请参见屏幕截图:

3。 在 复制并插入行和列 对话框中选择 复制并插入行 在选项 Type 部分,然后选择要复制的数据范围,然后指定重复时间以复制行,请参见屏幕截图:

4。 然后,点击 Ok or 使用 按钮,您将根据需要获得以下结果:

点击下载Kutools for Excel并立即免费试用!

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (32)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Halo

Ik heb een zoek programma gemaakt met VBA die de cel van het gezocht item groen maakt
Bijv. saldo 1 euro komt 3x voor in een blad ( B2 t/m F16 )
Dan maakt hij 3x de cel Saldo 1 euro groen
Nou zou ik graag willen dat hij deze 3 items kopieert op hetzelfde blad in kolom H
Wie weet hoe je dit moet schrijven in een Macro

B.v.d. Michel
This comment was minimized by the moderator on the site
Bonjour,
J'ai pu tester ce code (celui en haut du post) pour dupliquer plusieurs lignes autant de fois que le nombre présent dans la dernière colonne. Cela fonctionne très bien, Merci.
Par contre, dans une des colonnes, il y a une date de début. Je voudrais que cette date s'incrémente automatiquement lors de la duplication.
Pouvez vous m'aider s'il vous plait, je ne trouve pas la réponse sur internet ?
Merci par avance.
This comment was minimized by the moderator on the site
It took more time to make google understand what I want, But after I found this page, it takes a few minutes to get what I was searching for. Many thanks 👍
This comment was minimized by the moderator on the site
I desire to have the VBA Code for Copy And Insert Each Row Multiple Times Based On A Specific Number , please assist, Thank you
This comment was minimized by the moderator on the site
Hi, Carl,To copy and insert the rows multiple times based on specific numbers, please apply the below code:<div data-tag="code">Sub CopyRow()
'UpdatebyExtendoffice
Dim xRg As Range
Dim xCRg As Range
Dim xFNum As Integer
Dim xRN As Integer
On Error Resume Next
SelectRange:
xTxt = ActiveWindow.RangeSelection.Address
Set xRg = Application.InputBox("Select the list of numbers to copy the rows based on: ", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub

If xRg.Columns.Count > 1 Then
MsgBox "Please select single column!"
GoTo SelectRange
End If
Application.ScreenUpdating = False
For xFNum = xRg.Count To 1 Step -1
Set xCRg = xRg.Item(xFNum)
xRN = CInt(xCRg.Value)
With Rows(xCRg.Row)
.Copy
.Resize(xRN).Insert
End With
Next
Application.ScreenUpdating = True
End SubPlease try, hope it can help you!
This comment was minimized by the moderator on the site
skyyang not work!
This comment was minimized by the moderator on the site
This code works great. I have a situation where I am using Excel as a quote form. Worksheet one is the actual bid, and worksheet two is our cost page. When I insert the copied rows, I need it to do so on both pages. I have tried adding some code to select both pages, but it does not seem to work. Any help is greatly appreciated.
This comment was minimized by the moderator on the site
For the second VBA code (VBA code: Duplicate each row multiple times) I keep getting as run time error: 1004
This comment was minimized by the moderator on the site
Hello, is it possible to use different sizes(values) for each row? I am trying using vector, but it is not working.
For instance :
Rows(I).Resize(xCount(y) ).Insert

Where the values saved in xCount(y) are read from a table.
This comment was minimized by the moderator on the site
Hello, Guss,
May be the below article can help you, please check it:
https://www.extendoffice.com/documents/excel/4054-excel-duplicate-rows-based-on-cell-value.html
This comment was minimized by the moderator on the site
hi everyone.. Thank you in advance for you help!!

The VBA code to duplicate one specific row multiple times work perfectly until you use a filter. I wonder if someone can help me to solve this issue. I need a code that work even if you have some values filtered. I am using a large amount of data that is sort by locations. The code partially work, it pastes the number of row desire but no data or format when I have applied a filter.
This comment was minimized by the moderator on the site
No funciona con Windows 2019, deseo usar la función en mención pero simplemente no hace absolutamente nada
This comment was minimized by the moderator on the site
Copy And Insert Each Row Multiple Times With VBA Code

In VBA code how can I select the starting row
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations