如何在Excel 2013/2016中通过使用VBA宏脚本来只复制选中区域中的内容和格式
时间:2022-07-05 14:16:08
本文将讲述如何只将选中的数据区域中的内容和格式到Excel中的另一个区域。如何在Excel 2013/2016中通过使用VBA宏脚本来只复制选中区域中的内容和格式。
通过选择性粘贴来只复制值和格式
通过使用VBA脚本来只复制值和格式
假设您要将选定区域中的所有单元格复制到另一个区域中,并且您只想在复制所选单元格范围内内容值和格式。那么您可以使用“选择性粘贴”功能或使用新的VBA宏来完成复制。
通过选择性粘贴来只复制值和格式
要在Excel中仅将值和格式从一个选中区域复制到另一个区域,您可以执行以下步骤:
步骤1:选择一个想要复制的区域,然后按键盘上的Ctrl + C键复制选定的单元格。
步骤2:选择一个要放置拷贝内容的的空白单元格,例如:Cell E1。右键单击它,从右键弹出菜单中选择“选择性粘贴”,然后单击“值和源格式”。
步骤3:您将看到所有值和格式都已复制到另一个范围。
通过使用VBA脚本来只复制值和格式
您还可以使用Excel VBA宏仅将选定范围的单元格中的值和格式复制到另一个区域中。只需执行以下步骤:
步骤1:打开您的Excel工作簿,然后单击开发工具选项卡下的“Visual Basic”命令,或者只需按“ALT + F11”快捷方式。
步骤2:然后将出现“Visual Basic编辑器”窗口。
步骤3:单击“插入” – >“模块”以创建新模块。
步骤4:将以下VBA代码粘贴到代码窗口中。然后单击“保存”按钮。
Sub CopyOnlyValuesAndFormat() Set selectedRange = Application.Selection Set selectedRange = Application.InputBox("Select one range that you want to copy :", "CopyOnlyValuesAndFormat", selectedRange.Address, Type:=8) Set dRange = Application.InputBox("Select one blank Cell to palce values:", "CopyOnlyValuesAndFormat", Type:=8) selectedRange.Copy dRange.Parent.Activate dRange.PasteSpecial xlPasteValuesAndNumberFormats dRange.PasteSpecial xlPasteFormats Application.CutCopyMode = FalseEnd Sub
Sub CopyOnlyValuesAndFormat()
Set selectedRange = Application.Selection
Set selectedRange = Application.InputBox("Select one range that you want to copy :", "CopyOnlyValuesAndFormat", selectedRange.Address, Type:=8)
Set dRange = Application.InputBox("Select one blank Cell to palce values:", "CopyOnlyValuesAndFormat", Type:=8)
selectedRange.Copy
dRange.Parent.Activate
dRange.PasteSpecial xlPasteValuesAndNumberFormats
dRange.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End Sub
Sub CopyOnlyValuesAndFormat() Set selectedRange = Application.Selection Set selectedRange = Application.InputBox("Select one range that you want to copy :", "CopyOnlyValuesAndFormat", selectedRange.Address, Type:=8) Set dRange = Application.InputBox("Select one blank Cell to palce values:", "CopyOnlyValuesAndFormat", Type:=8) selectedRange.Copy dRange.Parent.Activate dRange.PasteSpecial xlPasteValuesAndNumberFormats dRange.PasteSpecial xlPasteFormats Application.CutCopyMode = FalseEnd Sub
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
iOS16有哪些新功能?iOS16有哪些BUG?
![](https://img.aspxhome.com/file/2023/1/45321_0s.png)
在Word2013中设置SmartArt位置的方法
![](https://img.aspxhome.com/file/2023/0/20880_0s.jpg)
word 如何进行个性化的设置
![](https://img.aspxhome.com/file/2023/7/35587_0s.png)
WPS中怎么画棱台图形
Excel如何取消条件格式设置
![](https://img.aspxhome.com/file/2023/7/55157_0s.gif)
花一分钟时间学习,长久受益的Excel小技巧
![](https://img.aspxhome.com/file/2023/7/39157_0s.gif)
在Word2016文档中如何插入特殊符号
win10电脑休眠唤醒快捷键介绍?win10电脑休眠唤醒快捷键有哪几个?
![](https://img.aspxhome.com/file/2023/3/47563_0s.png)
Win10无法更改账户名称怎么办?
![](https://img.aspxhome.com/file/2023/2/50532_0s.jpg)
Excel2016怎么快速重命名图表名称?
![](https://img.aspxhome.com/file/2023/7/41387_0s.jpg)
最新版Office 2016 带来黑色主题 如何使用
![](https://img.aspxhome.com/file/2023/4/16384_0s.jpg)
Win10系统EXE格式电子书打不开怎么办?EXE电子书无法打开解决方法
![](https://img.aspxhome.com/file/2023/5/52085_0s.png)
为WORD添加背景
![](https://img.aspxhome.com/file/2023/5/22225_0s.gif)
Word表格如何去边框
![](https://img.aspxhome.com/file/2023/6/18196_0s.png)
如何在Word 2013中更改垂直间距?
![](https://img.aspxhome.com/file/2023/1/23351_0s.jpg)
word如何固定图片大小
![](https://img.aspxhome.com/file/2023/0/34470_0s.gif)
win10商店下载位置怎么改成不是下载到C盘的方法?win10商店下载保存位置更改教程?
![](https://img.aspxhome.com/file/2023/2/47272_0s.jpg)
如何统一调整word文档中所有表格的宽度和高度?
![](https://img.aspxhome.com/file/2023/4/35864_0s.png)
Win10笔记本电脑投屏怎么扩展到大屏幕?
![](https://img.aspxhome.com/file/2023/0/53070_0s.jpg)
Word 15变身阅读器:增书签且兼容PDF
![](https://img.aspxhome.com/file/2023/2/21922_0s.jpg)