如何从另一个工作簿(excel)复制数据? [英] How to copy data from another workbook (excel)?
问题描述
首先我想复制到标题,但我不能让它工作 - 不断收到错误。
Sub CopyData(sheetName as String)
Dim File as String,SheetData as String
File =my file.xls
SheetData =数据的名称
#复制标题在主文件中的sheetName
工作簿(文件).Worksheets(SheetData).Range(A1)。在此选择#失败:方法选择类范围失败
工作簿(文件).Worksheets(SheetData) .Range(Selection,Selection.End(xlToRight))。选择
工作簿(文件).Worksheets(SheetData).Selection.Copy ActiveWorkbook.Sheets(sheetName).Cells(1,1)
End Sub
有什么问题?
我真的想要避免使我的file.xls有效。
编辑:我不得不放弃,复制SheetData将目标文件作为新工作表,然后才能工作。
查找并选择多行
最佳做法是打开源文件(如果不想打扰,则显示虚假的可见状态)阅读您的数据,然后关闭它。
以下链接可以使用工作和干净的代码:
http:// vba-有用的.blogspot.fr/2013/12/how-do-i-retrieve-data-from-another.html
I already have a macro that creates sheets and some other stuff. After a sheet has been created do I want to call another macro that copies data from a second excel (its open) to first and active excel file.
First I want to copy to headers, but I cant get that to work - keep getting errors.
Sub CopyData(sheetName as String)
Dim File as String, SheetData as String
File = "my file.xls"
SheetData = "name of sheet where data is"
# Copy headers to sheetName in main file
Workbooks(File).Worksheets(SheetData).Range("A1").Select # fails here: Method Select for class Range failed
Workbooks(File).Worksheets(SheetData).Range(Selection, Selection.End(xlToRight)).Select
Workbooks(File).Worksheets(SheetData).Selection.Copy ActiveWorkbook.Sheets(sheetName).Cells(1, 1)
End Sub
What is wrong ?
I really want to avoid having to make "my file.xls" active.
Edit: I had to give it up and copy the SheetData to target file as a new sheet, before it could work. Find and select multiple rows
Best practice is to open the source file (with a false visible status if you don't want to be bother) read your data and then we close it.
A working and clean code is avalaible on the link below :
http://vba-useful.blogspot.fr/2013/12/how-do-i-retrieve-data-from-another.html
这篇关于如何从另一个工作簿(excel)复制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!