简体   繁体   English

2个工作簿之间的数据传输

[英]Data Transfer between 2 workbooks

I want to transfer data from one workbook to another. 我想将数据从一个工作簿传输到另一个工作簿。 The destination workbook is changing it's name every day according to the date. 目标工作簿每天都会根据日期更改其名称。 I used this code: 我使用以下代码:

Dim MyMonth,MyYear,MyCage

 MyMonth = MonthName(Month(Date))
 MyYear = Year(Date)
 MyCage = "MainTest" & " " & MyDate
 My Date=Format(Date,"dd-mm-yyyy")

Dim MyFile As Workbook

Set MyFile = Workbooks.Open("C:\VBA1MC\MyYear\TestMC\MyMonth\MyCage")` 

It gives me a runtime error 1004. But when I change the 3 variables(MyYear,MyMonth,MyCage) to 2018,February,MainTest 08-02-2018 which gives me: 它给了我一个运行时错误1004。但是当我将3个变量(MyYear,MyMonth,MyCage)更改为2018年2月,MainTest,2018年2月8日,这给了我:

'Set MyFile = Workbooks.Open("C:\VBA1MC\2018\TestMC\February\MainTest 08-02-2018")'

it works. 有用。 What did I do wrong with the variables? 我对变量做了什么错?

Try the following code 试试下面的代码

Sub TestIt() 子TestIt()

Dim MyCage as string
Dim curDte As Date
Dim myDate As String, MyMonth As String, MyYear As String

MyMonth = MonthName(Month(Date))
MyYear = Year(Date)

curDte = Now
myDate = Format(curDte, "DD-MM-YYYY")
MyCage = "MainTest" & " " & myDate
Dim MyFile As Workbook

Set MyFile = Workbooks.Open("C:\VBA1MC\" & MyYear & "\TestMC\" & MyMonth & "\" & MyCage)

End Sub 结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM