VBA实现将文件夹“跨境”中工作簿“跨境风险配置”中工作表“平仓合约”“开仓存续”复制粘贴到文件夹“核对1”中工作簿“核对”中的工作表“平仓合约1”、“开仓存续1”
时间: 2023-11-04 19:06:13 浏览: 32
以下是实现上述要求的VBA代码:
Sub CopySheets()
Dim sourceFolder As String
Dim targetFolder As String
Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim sourceSheet1 As Worksheet
Dim sourceSheet2 As Worksheet
Dim targetSheet1 As Worksheet
Dim targetSheet2 As Worksheet
'设置源文件夹路径和目标文件夹路径
sourceFolder = "C:\跨境\"
targetFolder = "C:\核对1\"
'打开源工作簿和目标工作簿
Set sourceWorkbook = Workbooks.Open(sourceFolder & "跨境风险配置.xlsx")
Set targetWorkbook = Workbooks.Open(targetFolder & "核对.xlsx")
'获取源工作表
Set sourceSheet1 = sourceWorkbook.Sheets("平仓合约")
Set sourceSheet2 = sourceWorkbook.Sheets("开仓存续")
'获取目标工作表
Set targetSheet1 = targetWorkbook.Sheets("平仓合约1")
Set targetSheet2 = targetWorkbook.Sheets("开仓存续1")
'复制源工作表到目标工作表
sourceSheet1.Copy before:=targetSheet1
sourceSheet2.Copy before:=targetSheet2
'关闭源工作簿和目标工作簿
sourceWorkbook.Close SaveChanges:=False
targetWorkbook.Close SaveChanges:=True
'释放对象变量
Set sourceSheet1 = Nothing
Set sourceSheet2 = Nothing
Set targetSheet1 = Nothing
Set targetSheet2 = Nothing
Set sourceWorkbook = Nothing
Set targetWorkbook = Nothing
'显示完成消息框
MsgBox "已完成复制操作。"
End Sub
请将上述代码复制到一个新的VBA模块中,然后运行该宏即可实现所需功能。需要注意的是,代码中的文件夹路径和工作簿名称需要根据实际情况进行修改。另外,如果目标工作簿中已经存在同名的工作表,复制操作会将源工作表插入到同名工作表之前。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)