如何在 Excel 中仅复制选定区域的边框?
在使用 Microsoft Excel 时,通过“选择性粘贴”功能,可以轻松从一个区域中仅复制单元格的值、格式、公式等内容。但您是否尝试过在 Excel 中仅复制某个区域的边框样式呢?本文将向您展示如何在 Excel 中将选定区域的边框复制到新的区域的方法。
使用 VBA 代码仅复制选定区域的边框
以下 VBA 代码可以帮助您仅复制选定单元格的边框样式,并将其应用到 Excel 中的新区域。请按照以下步骤操作:
1. 按下“Alt” + “F11”键打开“Microsoft Visual Basic for Applications”窗口。
2. 在“Microsoft Visual Basic for Applications”窗口中,点击“插入” > “模块”,然后将以下 VBA 代码复制并粘贴到代码窗口中。
VBA 代码:仅复制 Excel 中选定区域的边框
Sub CopyBorders()
'Updated by Extendoffice 20211203
Dim xRg, yRg As Range
On Error Resume Next
Set xRg = Application.InputBox("Select Range with Borders to Copy...", "Kutools For Excel", , , , , , 8)
Set yRg = Application.InputBox("Select Cell to Apply Borders to range..", "Kutools For Excel", , , , , , 8)
With yRg.Borders(xlEdgeLeft)
.LineStyle = xRg.Borders(xlEdgeLeft).LineStyle
.ColorIndex = xRg.Borders(xlEdgeLeft).ColorIndex
.TintAndShade = xRg.Borders(xlEdgeLeft).TintAndShade
.Weight = xRg.Borders(xlEdgeLeft).Weight
End With
With yRg.Borders(xlEdgeTop)
.LineStyle = xRg.Borders(xlEdgeTop).LineStyle
.ColorIndex = xRg.Borders(xlEdgeTop).ColorIndex
.TintAndShade = xRg.Borders(xlEdgeTop).TintAndShade
.Weight = xRg.Borders(xlEdgeTop).Weight
End With
With yRg.Borders(xlEdgeBottom)
.LineStyle = xRg.Borders(xlEdgeBottom).LineStyle
.ColorIndex = xRg.Borders(xlEdgeBottom).ColorIndex
.TintAndShade = xRg.Borders(xlEdgeBottom).TintAndShade
.Weight = xRg.Borders(xlEdgeBottom).Weight
End With
With yRg.Borders(xlEdgeRight)
.LineStyle = xRg.Borders(xlEdgeRight).LineStyle
.ColorIndex = xRg.Borders(xlEdgeRight).ColorIndex
.TintAndShade = xRg.Borders(xlEdgeRight).TintAndShade
.Weight = xRg.Borders(xlEdgeRight).Weight
End With
With yRg.Borders(xlInsideHorizontal)
.LineStyle = xRg.Borders(xlInsideHorizontal).LineStyle
.ColorIndex = xRg.Borders(xlInsideHorizontal).ColorIndex
.TintAndShade = xRg.Borders(xlInsideHorizontal).TintAndShade
.Weight = xRg.Borders(xlInsideHorizontal).Weight
End With
With yRg.Borders(xlInsideVertical)
.LineStyle = xRg.Borders(xlInsideVertical).LineStyle
.ColorIndex = xRg.Borders(xlInsideVertical).ColorIndex
.TintAndShade = xRg.Borders(xlInsideVertical).TintAndShade
.Weight = xRg.Borders(xlInsideVertical).Weight
End With
End Sub
3. 按下“F5”键运行代码。在弹出的第一个“Kutools for Excel”对话框中,请选择需要复制边框的区域,然后点击“确定”按钮。
4. 在第二个“Kutools for Excel”对话框中,请选择一个单元格以应用复制的边框,然后点击“确定”按钮。请参见截图:
然后,您可以发现选定区域的边框样式已被复制并应用到新区域,如下图所示。
相关文章:
- 如何在 Excel 中复制并粘贴值时跳过重复项?
- 如何在 Excel 中复制单元格数据的同时保留行高和列宽?
- 如何在 Excel 中将单元格作为文本值而不是公式进行复制?
- 如何在 Excel 中将数据复制到另一个工作表的下一个空白行?
最佳Office办公效率工具
🤖 | Kutools AI 助手:以智能执行为基础,彻底革新数据分析 |代码生成 |自定义公式创建|数据分析与图表生成 |调用Kutools函数…… |
热门功能:查找、选中项的背景色或标记重复项 | 删除空行 | 合并列或单元格且不丢失数据 | 四舍五入…… | |
高级LOOKUP:多条件VLookup|多值VLookup|多表查找|模糊查找…… | |
高级下拉列表:快速创建下拉列表 |依赖下拉列表 | 多选下拉列表…… | |
列管理器: 添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域与列…… | |
特色功能:网格聚焦 |设计视图 | 增强编辑栏 | 工作簿及工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按名单发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/倾斜/删除线等)…… | |
15大工具集:12项 文本工具(添加文本、删除特定字符等)|50+种 图表 类型(甘特图等)|40+实用 公式(基于生日计算年龄等)|19项 插入工具(插入二维码、从路径插入图片等)|12项 转换工具(小写金额转大写、汇率转换等)|7项 合并与分割工具(高级合并行、分割单元格等)| …… |
通过Kutools for Excel提升您的Excel技能,体验前所未有的高效办公。 Kutools for Excel提供300多项高级功能,助您提升效率并节省时间。 点击此处获取您最需要的功能……
Office Tab为Office带来多标签界面,让您的工作更加轻松
- 支持在Word、Excel、PowerPoint中进行多标签编辑与阅读。
- 在同一个窗口的新标签页中打开和创建多个文档,而不是分多个窗口。
- 可提升50%的工作效率,每天为您减少数百次鼠标点击!
所有Kutools加载项,一键安装
Kutools for Office套件包含Excel、Word、Outlook和PowerPoint的插件,以及Office Tab Pro,非常适合跨Office应用团队使用。





- 全能套装——Excel、Word、Outlook和PowerPoint插件+Office Tab Pro
- 单一安装包、单一授权——数分钟即可完成设置(支持MSI)
- 协同更高效——提升Office应用间的整体工作效率
- 30天全功能试用——无需注册,无需信用卡
- 超高性价比——比单独购买更实惠