KutoolsforOffice — 一套方案,五大工具。事半功倍。

如何仅复制 Excel 中所选区域的边框?

作者Siluvia修改日期

使用 Microsoft Excel 时,借助“选择性粘贴”功能,您可以轻松地仅复制单元格的值、格式或公式等内容。但您是否尝试过仅复制某个区域的边框样式?本文将为您演示如何将选定区域的边框样式单独复制到 Excel 中的新区域。

使用 VBA 代码仅复制选择区域的边框


使用 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”对话框中,选择您需要复制边框的区域,然后点击“确定”按钮。

一张截图,展示在 Excel 中使用 VBA 代码选择带有边框的区域以进行复制

4. 在第二个“Kutools for Excel”对话框中,请选择一个要应用已复制边框的单元格,然后单击“确定”按钮。参见截图:

一张截图,展示在 Excel 中选择要应用已复制边框的单元格

随后您将看到,所选区域的边框样式已成功复制并应用到新区域,如下方截图所示。

一张截图,展示在新区域上应用的边框样式


相关文章:


最佳办公效率工具

🤖KUTOOLS AI 助手:基于以下内容革新数据分析:智能执行   |  生成代码|  创建自定义公式  |  数据分析及生成图表|  调用 Kutools Functions……
热门功能查找、高亮或标记重复项   |  删除空白行   |  合并列或单元格且不丢失数据   |  不使用公式的四舍五入……
高级 LOOKUP多条件 VLookup  |  多值 VLookup  |   跨多工作表 VLookup   |   模糊查找……
高级下拉列表快速创建下拉列表   |  级联下拉列表   |  多选下拉列表……
列管理器添加指定数量的列|移动列|切换隐藏列的可见性状态|比较区域与列……
特色功能网格聚焦   |  设计视图   |增强编辑栏   | 工作簿和表管理器   |  资源库(自动文本)|  日期提取   |  汇总工作表  |  加密/解密单元格   | 按列表发送邮件   |  超级筛选   |   特殊筛选(筛选粗体单元格/斜体/删除线……) ......
精选 15 工具集12 文本工具添加文本删除特定字符,……)|   50+ 图表 类型甘特图,……)|   40+ 实用公式基于生日计算年龄,……)|   19 插入工具插入二维码从路径插入图片,……)|   12 转换工具小写金额转大写汇率转换,……)|   7 合并和拆分工具高级合并行分割单元格,……)|……更多
在您的首选语言中使用 Kutools – 支持英语、西班牙语、德语、法语、中文及 40+ 种其他语言!

使用 Kutools for Excel 大幅提升您的 Excel 技能,体验前所未有的高效。Kutools for Excel 提供 300 多项高级功能,助您提升生产力、节省时间。立即点击此处,获取您最需要的功能……


Office Tab 为 Office 带来标签式界面,让您的工作更轻松

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中启用标签式编辑和阅读
  • 在同一个窗口的新标签页中打开并创建多个文档,而非在新窗口中。
  • 将您的工作效率提升 50%,每天减少数百次鼠标点击!

所有 Kutools 插件,一个安装程序

Kutools for Office 套件捆绑了适用于 Excel、Word、Outlook 和 PowerPoint 的插件以及 Office Tab Pro,非常适合需要跨多个 Office 应用高效协作的团队。

ExcelWordOutlookTabsPowerPoint
  • 一体化套件— Excel、Word、Outlook 和 PowerPoint 插件 + Office Tab Pro
  • 一个安装程序,一个许可证— 几分钟内完成设置(支持 MSI)
  • 协同效果更佳— 在多个 Office 应用中实现高效协同
  • 30 天全功能试用— 无需注册,无需信用卡
  • 超值之选— 比单独购买插件更省钱