我想根据场景复制其他工作簿的变量内容(例如:我想从Workbook data2013或data2014复制数据等)。通过使用以下宏,我已经能够使用特定的工作簿。现在我希望“\ data2013.xls”根据场景进行更改。我已经创建了一个函数,它将为Cell生成正确的路径:RawData!$ A $ 2。我以为我可以使用变量,但我已经退出了VBA的新功能,你能帮帮我吗?
Sheets("RawData").Select
Workbooks.Open Filename:=ThisWorkbook.Path & "\data2013.xls"
Sheets("Documents").Select
ActiveSheet.Unprotect
ActiveSheet.ShowAllData
Range("$A$4:$T$65536").Select
Selection.Copy
Windows("Reports.xls").Activate
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
答案 0 :(得分:0)
' don't use 655536 as after Excel 2007 there will be gazillions of possible rows...
Range("$A$4:$T$" & activesheet.rows.count).Copy Destination:=workbooks("Reports.xls").range("C1")
Sheets("Documents").Unprotect
ActiveSheet.ShowAllData
Range("$A$4:$T$65536").Copy Destination:=workbooks("Reports.xls").range("C1")
接下来,使用变量作为源数据首先声明它:
dim sSrcBook as string
然后设置它的值:
sSrcBook="\data2013.xls"
然后使用它
Workbooks.Open Filename:=ThisWorkbook.Path & sSrcBook
Sheets("Documents").Unprotect
ActiveSheet.ShowAllData
' don't use 655536 as after Excel 2007 there will be gazillions of possible rows...
Range("$A$4:$T$" & activesheet.rows.count).Copy Destination:=workbooks("Reports.xls").range("C1")
workbooks(sSrcBook").close false ' close without saving changes
您可以遍历一系列单元格,从而获得源工作簿变量的不同路径
希望这有帮助,如果您需要更多帮助,请编辑您的问题或发表评论