我有一份与我合作的工作簿。我需要从另一个工作簿导入数据。第一个工作簿称为MyBook.xls,第二个工作簿称为protfolioData.xls。 protfolio数据的路径在单元格D12中。如何创建一个采用D12路径的vbscript,并从protfolio数据中复制A4:D26单元格中的数据,并在MyBook.xls中创建一个newSheet。我目前正在尝试以下操作,当从MyBook.xls运行宏时,它会抛出一个错误的ambigous名称。
ext_file = Workbooks(mainFile).Worksheets("config").Range("D12").Value
Workbooks(ext_file).Sheets("Sheet1").Rows("4:26").Copy _
Workbooks(mainFile).Sheets("Sheet2").Rows("1:22")
答案 0 :(得分:2)
也许请尝试以下代码:
Sub test()
'Get the Path
ext_file = ThisWorkbook.Sheets("config").Range("D12").Value
'Get the workbook name from the path
GetFileName = Right(ext_file, Len(ext_file) - InStrRev(ext_file, "\"))
'open and activate the workbook
Workbooks.Open Filename:=ext_file
Workbooks(GetFileName).Activate
'Add new sheet to thisworkbook ( MyBook.xls )
With ThisWorkbook
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
End With
'Copy rows 4 to 26 from protfolioData.xls and paste in rows 1 to 23 in ( MyBook.xls )
Workbooks(GetFileName).Sheets("Sheet1").Rows("4:26").Copy _
Destination:=ws.Rows("1:23")
'close protfolioData.xls
Workbooks(GetFileName).Close savechanges:=False
End Sub