跳到主要内容

如何在Excel中以固定间隔插入特定数量的行?

在Excel工作表中,可以使用插入功能在现有行之间插入空白行。 但是,如果您有大量数据,并且需要在每第三个或第n行之后插入两个空白行,那么如何快速便捷地完成此工作?


使用VBA代码以固定的间隔将特定数量的空白行插入数据范围

以下VBA代码可以帮助您在现有数据中的第n行之后插入特定数量的行。 请执行以下操作:

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

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

VBA代码:以固定的间隔将特定数量的行插入数据

Sub InsertRowsAtIntervals()
'Updateby Extendoffice
Dim Rng As Range
Dim xInterval As Integer
Dim xRows As Integer
Dim xRowsCount As Integer
Dim xNum1 As Integer
Dim xNum2 As Integer
Dim WorkRng As Range
Dim xWs As Worksheet
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
xRowsCount = WorkRng.Rows.Count
xInterval = Application.InputBox("Enter row interval. ", xTitleId, 1, Type:=1)
xRows = Application.InputBox("How many rows to insert at each interval? ", xTitleId, 1, Type:=1)
xNum1 = WorkRng.Row + xInterval
xNum2 = xRows + xInterval
Set xWs = WorkRng.Parent
For i = 1 To Int(xRowsCount / xInterval)
    xWs.Range(xWs.Cells(xNum1, WorkRng.Column), xWs.Cells(xNum1 + xRows - 1, WorkRng.Column)).Select
    Application.Selection.EntireRow.Insert
    xNum1 = xNum1 + xNum2
Next
End Sub

3. 粘贴此代码后,请按 F5 键以运行此代码,将弹出一个提示框,提醒您选择要插入空白行的数据范围,请参见屏幕截图:

4。 点击 OK 按钮,将弹出另一个提示框,请输入行间隔的数量,请参见屏幕截图:

5。 继续点击 OK 按钮,在下面弹出的提示框中,请输入要插入的空白行数,请参见屏幕截图:

6。 然后点击 OK,并且空白行已定期插入到现有数据中,请参见屏幕截图:


使用VBA代码根据单元格值将特定数量的空白行插入数据范围

有时,您可能需要根据单元格值列表插入空白行,在这种情况下,下面的VBA代码可以帮您一个忙,请这样做:

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

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

VBA代码:根据数字列表插入特定数量的空白行:

Sub Insertblankrowsbynumbers ()
'UpdatebyExtendoffice
Dim xRg As Range
Dim xAddress As String
Dim I, xNum, xLastRow, xFstRow, xCol, xCount As Long
On Error Resume Next
xAddress = ActiveWindow.RangeSelection.Address
Set xRg = Application.InputBox("Select the spefic number column to use(single column):", "Kutools For Excel", xAddress, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
xLastRow = xRg(1).End(xlDown).Row
xFstRow = xRg.Row
xCol = xRg.Column
xCount = xRg.Count
Set xRg = xRg(1)
For I = xLastRow To xFstRow Step -1
xNum = Cells(I, xCol)
If IsNumeric(xNum) And xNum > 0 Then
Rows(I + 1).Resize(xNum).Insert
xCount = xCount + xNum
End If
Next
xRg.Resize(xCount, 1).Select
Application.ScreenUpdating = True
End Sub

3。 粘贴此代码后,按 F5 键以运行此代码,在弹出的对话框中,选择要基于其插入空白行的数字列表,请参见屏幕截图:

4。 然后,点击 OK,您将获得所需的结果,如以下屏幕截图所示:


使用方便的功能以固定的间隔将特定数量的空白行插入数据范围

如果您不熟悉上述VBA代码, Kutools for Excel 也可能对您有帮助 插入空白行和列 该功能可以按固定的间隔快速轻松地将特定数量的行或列插入现有数据。

请注意:申请这个 插入空白行和列 ,首先,您应该下载 Kutools for Excel,然后快速轻松地应用该功能。

安装后 Kutools for Excel,请执行以下操作:

1。 选择要间隔插入空白行的数据范围。

2。 点击 库工具 > 插页 > 插入空白行和列,请参见屏幕截图:

3。 在 插入空白行和列 对话框,选择 空白行 来自的选项 插入类型,然后指定要使用的间隔和空白行的数量,如下所示的屏幕截图:

4。 然后点击 OK 按钮,并且空白行已以特定间隔插入到选定范围中,如以下屏幕截图所示:

立即下载和免费试用Excel的Kutools!


使用VBA代码根据特定编号多次复制和插入行

假设您有一个tada范围,现在,您想复制每行并将其多次粘贴到下一个行,这些行基于数字列表,如下所示的屏幕截图。 如何在Excel工作表中解决此任务?

为了处理此工作,我将为您介绍一个有用的代码,请执行以下步骤:

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

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

VBA代码:根据特定数字多次复制和插入行:

Sub CopyRows()
'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 Sub

3。 粘贴此代码后,按 F5 键以运行此代码,在弹出的对话框中,选择要复制的数字列表并基于其插入数据行,请参见屏幕截图:

4。 然后,点击 OK 按钮,并且已将特定的行数复制并粘贴到每个原始行下,请参见屏幕截图:


具有特定功能,可根据特定数字多次复制和插入行

如果你有 Kutools for Excel,其 根据单元格值复制行/列 功能,您可以根据数字列表快速轻松地插入行或列。

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

安装后 Kutools for Excel,请执行以下操作:

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

2。 在 复制并插入行和列 对话框中选择 复制并插入行 在选项 Type 部分,然后选择要复制的数据范围,然后指定要基于其复制行的值列表,请参见屏幕截图:

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

立即下载和免费试用Excel的Kutools!

更多相关文章:

  • 多次复制和插入行或复制X次
  • 在日常工作中,您是否曾经尝试过复制一行或每一行,然后在工作表中当前数据行的下方多次插入? 例如,我有一个单元格区域,现在,我想复制每一行并将它们粘贴3次到下一行,如下图所示。 您如何在Excel中处理这项工作?
  • 在Excel中值更改时插入空白行
  • 假设您具有一系列数据,现在您想在值更改时在数据之间插入空白行,以便可以将一列中的顺序相同值分开,如下面的屏幕截图所示。 在本文中,我将为您解决一些技巧。
  • 根据条件将多个工作表中的行复制到新工作表中
  • 假设您有一个包含三个工作表的工作簿,这些工作表的格式与下面的屏幕快照相同。 现在,您想要将这些工作表中的C列包含“已完成”文本的所有行复制到新工作表中。 您如何快速,轻松地解决此问题,而又不手动一一复制和粘贴它们?

最佳办公生产力工具

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

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

kte选项卡201905


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

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (39)
Rated 5 out of 5 · 2 ratings
This comment was minimized by the moderator on the site
Marvelous vba script!
I had over 5000 rows that i need to add new rows to in between. All other guides told me to make "helper" column it would take me good part of my life to add 1,2 copy paste over and over again just to add new rows.
So, Thanks for this!
Rated 5 out of 5
This comment was minimized by the moderator on the site
hola, hay algun codigo que me permita copiar los datos, pero que en la primera columna que son fechas puedan ser consecutivas.

ejemplo

en vez de que quede asi

10/01/2022 19.258.369-4 Juan Ramirez
10/01/2022 19.258.369-4 Juan Ramirez
10/01/2022 19.258.369-4 Juan Ramirez

quede asi

10/01/2022 19.258.369-4 Juan Ramirez
11/01/2022 19.258.369-4 Juan Ramirez
12/01/2022 19.258.369-4 Juan Ramirez

gracias
This comment was minimized by the moderator on the site
hola, hay algun codigo que me permita copiar los datos, pero que en la primera columna que son fechas pueda ser consecutivo.

ejemplo

en vez de que quede asi

01/10/2022 19.258.369-4 Juan Ramirez
01/10/2022 19.258.369-4 Juan Ramirez
01/10/2022 19.258.369-4 Juan Ramirez

quede asi

01/10/2022 19.258.369-4 Juan Ramirez
02/10/2022 19.258.369-4 Juan Ramirez
03/10/2022 19.258.369-4 Juan Ramirez

gracias
This comment was minimized by the moderator on the site
Name Email Phone Address
0 Name Email Phone Adress
address line 2 Name Phone 0
Name Email Phone Adress
0 Name Email Phone Adress
address line 2 0


How could I edit this to start a new row at every empty value or 0 value without having phone numbers with 0 start a new row?
This comment was minimized by the moderator on the site
Hello, Jarrod

Sorry, I can't get your problem clearly.
Could you explain your problem more detailed? Or you can insert a screenshot or file here.
Thank you!
This comment was minimized by the moderator on the site
Вот выручили так выручили!
Сидел, ломал голову как добавить строки по заданному количеству.
Ваш макрос мне очень помог.
Rated 5 out of 5
This comment was minimized by the moderator on the site
can you tell me how to insert column like this way , what is the code
This comment was minimized by the moderator on the site
Hello friend,
You can use this VBA code:

Sub InsertColumnsAtIntervals()

'Updateby Extendoffice

Dim Rng As Range

Dim xInterval As Integer

Dim xColumns As Integer

Dim xColumnsCount As Integer

Dim xNum1 As Integer

Dim xNum2 As Integer

Dim WorkRng As Range

Dim xWs As Worksheet

xTitleId = "KutoolsforExcel"

Set WorkRng = Application.Selection

Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)

xColumnsCount = WorkRng.Columns.Count

xInterval = Application.InputBox("Enter column interval. ", xTitleId, 1, Type:=1)

xColumns = Application.InputBox("How many columns to insert at each interval? ", xTitleId, 1, Type:=1)

xNum1 = WorkRng.Column + xInterval

xNum2 = xColumns + xInterval

Set xWs = WorkRng.Parent

For i = 1 To Int(xColumnsCount / xInterval)

    xWs.Range(xWs.Cells(WorkRng.Row, xNum1), xWs.Cells(WorkRng.Row, xNum1 + xColumns - 1)).Select

    Application.Selection.EntireColumn.Insert

    xNum1 = xNum1 + xNum2

Next

End Sub


Sincerely,
Mandy
This comment was minimized by the moderator on the site
Muito obrigado, salvou meu trabalho, eu não tinha ideia de como fazer. Muito obrigado mesmo!
This comment was minimized by the moderator on the site
Hello,
You are welcome. Glad it helps. Any questions, please feel free to contact us. Have a great day.
Sincerely,
Mandy
This comment was minimized by the moderator on the site
This is so Cool!! Thanks a lot
This comment was minimized by the moderator on the site
Can I get the VBA code for deletion of rows based on duplicate values in a selected column keeping all unique values?
This comment was minimized by the moderator on the site
Hello, Roy,If you want to remove rows based on duplicate values, normally, you can use the Remove Duplicates feature in Excel to remove the rows.Of course, if you need a VBA code, please use the below code: (First, you should select the data range that you want to remove, and then run this code, the rows based on the duplicate values in the first column of your selection will be removed at once. )<div data-tag="code">Sub Delete_duplicate_rows()
Dim Rng As Range
Set Rng = Selection
Rng.RemoveDuplicates Columns:=Array(1), Header:=xlYes
End SubPlease try, hope it can help you!
This comment was minimized by the moderator on the site
Thank you author! You deserve the best commendation for these! But please could you help me out with the code to put a constant value into all blank rows I created with your code above? To make myself more clearer, I need to insert a constant value into all blank rows (this solved already with your code above) then I need to insert a constant value into all of the blank rows (this is my problem). Thank you as I expect your kind response.
This comment was minimized by the moderator on the site
Hello, Do you mean to fill blank rows with specific value? If so, mabe the following article cna help you:https://www.extendoffice.com/documents/excel/772-excel-fill-blank-cells-with-0-or-specific-value.html
Please try it.
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