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

时间:2018-02-08 19:00:31

标签: excel-vba vba excel

我想将数据从一个工作簿传输到另一个工作簿。目标工作簿每天根据日期更改其名称。 我用了这段代码:

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")` 

它给我一个运行时错误1004。 但是当我将3个变量(MyYear,MyMonth,MyCage)更改为2018年2月,MainTest 08-02-2018时,它给了我:

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

它有效。我对变量做了什么错误?

1 个答案:

答案 0 :(得分:0)

尝试以下代码

Sub 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