跳到主要内容

Excel中值更改时如何插入空白行?

假设您具有一系列数据,现在您想在值更改时在数据之间插入空白行,以便可以将一列中的顺序相同值分开,如下面的屏幕截图所示。 在本文中,我将为您解决一些技巧。

使用小计功能更改值时插入空白行

值随辅助列更改时插入空白行

使用VBA代码更改值时插入空白行

功能强大的值更改时,插入特定数量的空白行


使用小计功能更改值时插入空白行

随着 产品总计 功能,您可以按以下步骤在值更改时在数据之间插入空白行:

1。 选择您要使用的数据范围。

2。 点击 时间 > 产品总计 打开 产品总计 对话框,然后在 产品总计 对话框,请执行以下选择:

1:根据值何时在以下更改,选择要插入空白行的列名 每次变化 在部分;
2:选择 计数 来自 使用功能 下拉列表;
3:检查要在其中插入小计的列名 将小计添加到 列表框

3。 然后点击 OK,小计线已插入到不同产品之间,并且大纲符号显示在表的左侧,请参见屏幕截图:

4。 然后单击轮廓符号顶部的数字2,仅显示小计线。

5。 然后选择小计范围数据,然后按 Alt +; 快捷键仅选择可见行,请参见屏幕截图:

6。 仅选择可见行后,然后按 删除 键,所有小计行都已删除。

7。 然后单击任何其他单元格,然后返回 时间 > 取消组合 > 清除轮廓 删除大纲,请参见屏幕截图:

8。 轮廓符号立即被清除,当值更改时,您可以看到在数据之间插入了空白行,请参见屏幕截图:

9。 最后,您可以根据需要删除列A。


值快速变化时插入分页符,空白行,底部边框或填充颜色

如果你有 Kutools for Excel's 区分差异 功能,您可以根据需要更改值时快速插入分页符,空白行,底部边框或填充颜色。 请参见下面的演示。         点击下载Kutools for Excel!


值随辅助列更改时插入空白行

使用帮助器列,您可以先插入公式,然后再应用 查找和替换 函数,最后,在变化的值之间插入空白行。 请执行以下操作:

1。 在空白单元格C3中,请输入此公式 = A3 = A2,然后在单元格D4中输入此公式 = A4 = A3,请参见屏幕截图:

2。 然后选择C3:D4,并将填充手柄拖到您要应用这些公式的范围内,您将获得 or 在单元格中,请参见屏幕截图:

3。 然后按 Ctrl + F 键打开 查找和替换 对话框,在弹出的对话框中,输入 FALSE查找内容 下的文字框 找到最适合您的地方 选项卡,然后单击 附加选项 按钮以展开此对话框,然后选择 价值观 来自 在看 下拉列表,请参见屏幕截图:

4. 点击 找到所有 按钮,然后按 按Ctrl + A 要选择所有查找结果,请一次选择所有FALSE单元,请参见屏幕截图:

6。 关上 查找和替换 对话框,下一步,您可以通过单击插入空白行 主页 > 插页 > 插入图纸行,并且当值基于A列更改时,空白行已插入到数据中,请参见屏幕截图:

7。 最后,您可以根据需要删除帮助程序列C和D。


使用VBA代码更改值时插入空白行

如果您厌倦了使用上述方法,下面的代码也可以帮助您立即在更改后的值之间插入空白行。

1。 按住 ALT + F11 键打开 Microsoft Visual Basic for Applications窗口.

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

VBA代码:值更改时插入空白行

Sub InsertRowsAtValueChange()
'Update by Extendoffice
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
For i = WorkRng.Rows.Count To 2 Step -1
    If WorkRng.Cells(i, 1).Value <> WorkRng.Cells(i - 1, 1).Value Then
        WorkRng.Cells(i, 1).EntireRow.Insert
    End If
Next
Application.ScreenUpdating = True
End Sub

3。 然后按 F5 键以运行此代码,然后会弹出一个提示框,让您选择要在基于值更改时插入空白行的一列数据,请参见屏幕截图:

4。 然后点击 OK,当值基于列A更改时,在数据之间插入了空白行。


功能强大的值更改时,插入特定数量的空白行

如果您尝试使用上述麻烦的方法,在这里,我将介绍一个有用的工具, Kutools for Excel's 区分差异 可以帮助您在单元格值快速而轻松地更改时插入分页符,空白行,底部边框或填充颜色。

提示:申请这个 区分差异 功能,首先,您应该下载 Kutools for Excel,然后快速轻松地应用该功能。

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

1。 点击 库工具 > 格式 > 区分差异,请参见屏幕截图:

2。 在 按关键列区分差异 对话框,请执行以下操作:

  • 选择要使用的数据范围,然后选择要基于其插入空白行的键列;
  • 然后检查 空白行 选项从 附加选项 部分,然后输入要插入的空白行数。

3。 然后点击 Ok 按钮,并且如果单元格值发生更改,则将特定数量的空白行插入到数据中,请参见屏幕截图:

点击下载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 (16)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi, Hassan,

This vba is amazing, whereas I need one more alternate code for insert single blank row after changes in sequencing numbers. Example:- In column having number series like 1, 2, 3, 5, 6, 9 & etc...
Need to add single blank row between 3 & 5 as well as 6 & 9.
Please can you with this.
This comment was minimized by the moderator on the site
Can the VBA method be adapted to ignore blank cells? I have a file which I need to insert rows in based on two different columns but when I run the macro on the second column I end up with three blank lines where the first macro run inserted rows.
Or can it run on two columns at the same time?
This comment was minimized by the moderator on the site
Hello,
Is very usefull in case i need to insert 1 row, but if i need to insert 145 rows in every time the data change in spwcific column, how can i do it??
This comment was minimized by the moderator on the site
Hi, Hassan,
To insert multiple blank rows when value changes in a specific column, you should apply the following VBA code:

Note: In the below code, you should change the number 99 to your need, for example, when you insert 145 blank rows, you should change the number 99 to 144. Please try it, hope it can help you!

Sub InsertRowsAtValueChange()
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
For i = WorkRng.Rows.Count To 2 Step -1
If WorkRng.Cells(i, 1).Value <> WorkRng.Cells(i - 1, 1).Value Then
Range(WorkRng.Cells(i, 1).EntireRow, WorkRng.Cells(i + 99, 1).EntireRow).Insert
End If
Next
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Very helpful. The code that skyyang shows above worked perfectly. Just make sure that the data doesn't already have spaces in it.

I don't understand VBA, but I believe if you wanted to add more rows underneath data that already had the spacing, there should be a way to ignore spaces.

Could a line be added to ignore or skip over blank lines? That might make this code more universal and repeatable if needed. Also a delete function that is similar to this may be useful so undo isn't necessary.
This comment was minimized by the moderator on the site
RE: insert blank rows when value changes with vba code
Is there a way that I can save the Range & not have to pick it every time I run it?
This comment was minimized by the moderator on the site
The VBA code worked 1st time and did exactly what I was trying to do. Thank you so much!
This comment was minimized by the moderator on the site
I've been using my own solution for some time.
1. Insert a helper column into Column A
2. In A2, type "if(B2=B1,A1,A1+1)"
3. Copy that formula down to the last row
4. Copy all the populated cells in column A and Paste Special (Values) over them
5. Copy all the cells again and paste them into column A in the first unpopulated cell (e.g. if you have 104 rows of data plus a header row you would paste into cell A106)
6. Click on Data and Remove Duplicates (only on the cells you just pasted in Step 5; not on all rows)
7. Sort all of Column A
8. Delete Column A

Seems like a lot of steps but only takes a few seconds.
This comment was minimized by the moderator on the site
[quote]Hi all thank you!! its awesome , can you guys also let me how to insert 2 rows when the value changes in VBA or through excel.By Hudson[/quote] Please let me know how to insert more than 1 row.
This comment was minimized by the moderator on the site
Hi there, These are almost useful! The first method doesn't work for me because when I follow the steps explicitly, the the data that I delete in the subtotal panes deletes the entire columns that I've sorted. In the second method when I get to the step where I insert sheet rows, the rows are inserted ABOVE the FALSE cells which breaks up the data, but the last selection of every group is then added to the group below. Any advice???
This comment was minimized by the moderator on the site
Hi all thank you!! its awesome , can you guys also let me how to insert 2 rows when the value changes in VBA or through excel.
This comment was minimized by the moderator on the site
Where in the code would I need to modify to include more than one row, I need to add 10 after each break... Thanks
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