我正在尝试将数据从一个工作簿传输到另一个工作簿。工作簿A有48个不同数据的选项卡,我有48个excel文件,我需要复制粘贴到特定列。选项卡名称和文件名是匹配的,这就是我设置名称范围的原因。我一直从open语句(设置y)
获得语法错误和ws1-select-range行的另一个错误。你会帮我理解我做错了吗?
5*5 = 25
'收到编译错误:预期结束语
Sub Transfer()
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim names As String
Set x = ThisWorkbook
For a = 2 To 49
names = ThisWorkbook.Sheets(1).Range("A" & a).Text
'更新了以下代码,然后得到了运行时错误' 1004':范围类的复制方法失败了
set y = workbook.Open Filename:=Thisworkbook.Path & "/" & names & ".xlsx"
Set ws1 = x.Sheets(names)
Set ws2 = y.Sheets("Equipment")
我感谢你的时间和建议。
答案 0 :(得分:0)
所以我更新了一些代码,并且有点让它成功。 :)
Sub Transfer()
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim names As String
Dim lastrow As Integer
Dim a As Integer
Set x = ThisWorkbook
For a = 2 To 49
names = ThisWorkbook.Sheets(1).Range("A" & a).Text
Set y = Workbooks.Open("C:\Documents\Work\" & names & ".xlsx")
Set ws1 = x.Sheets(names)
Set ws2 = y.Sheets(2)
lastrow = ws1.Cells(Rows.Count, 6).End(xlUp).Row
ws1.Range("B2:B" & lastrow).Copy ws2.Range("A3")
ws1.Range("C2:C" & lastrow).Copy ws2.Range("B3")
ws1.Range("F2:F" & lastrow).Copy ws2.Range("C3")
ws1.Range("G2:G" & lastrow).Copy ws2.Range("D3")
ws1.Range("H2:H" & lastrow).Copy ws2.Range("E3")
ws1.Range("I2:I" & lastrow).Copy ws2.Range("F3")
ws1.Range("J2:J" & lastrow).Copy ws2.Range("G3")
ws1.Range("L2:L" & lastrow).Copy ws2.Range("H3")
ws1.Range("M2:M" & lastrow).Copy ws2.Range("I3")
ws1.Range("N2:N" & lastrow).Copy ws2.Range("J3")
ws1.Range("O2:O" & lastrow).Copy ws2.Range("K3")
y.Save
y.Close
Next a
End Sub