[英]Copying and pasting from Macro workbook to multiple workbooks Excel
I am creating a macro that will process and format scans (SCANfile#.xlm) saved as .xlm files The end result should be a processed, formatted workbook that will be saved as an Excel file. 我正在创建一个宏,该宏将处理和格式化另存为.xlm文件的扫描(SCANfile#.xlm),最终结果应该是经过处理的格式化工作簿,该工作簿将另存为Excel文件。 I am creating the macro in a workbook I will call SCANMacro for the purpose of this discussion with the intention of it being open simultaneously with whichever SCANfile# workbook I may have open at the time. 我正在工作簿中创建宏,出于讨论的目的,我将其称为SCANMacro,目的是与我当时打开的任何SCANfile#工作簿同时打开。
Sub Copy and Paste Data ()
'select cell on target workbook/worksheet
Range("X1").Select
'
'select the data from the saved workbook that the macro runs from
'
Columns("SCAMNmacro.xlsm C:G").Select
Selection.Copy
'
'Paste into SCANfile#.xlm.xml
'
Windows("SCANFILE01.xml").Activate
Range("X1").Select
ActiveSheet.Paste
End Sub
This works fabulously as long as I run it from the SCANfile01.xml I had opened as I created it. 只要我从创建它时打开的SCANfile01.xml中运行它,就可以很好地工作。 As soon as I open the Macro enabled workbook with a different scanfile, it errors out. 一旦我使用其他扫描文件打开启用了宏的工作簿,它就会出错。
How do I set this up so that the Macro works on any ScanSheet? 我该如何设置以便宏可在任何ScanSheet上使用? I tried working with Thisworkbook and Activeworkbook to no avail. 我尝试使用Thisworkbook和Activeworkbook均无济于事。 I am not a programmer so most of this has been recorded with me tweaking inside the VBA editor. 我不是程序员,因此大部分记录都是通过在VBA编辑器中进行调整来记录的。
Use Workbook variables for referencing the proper Workbooks: 使用工作簿变量来引用适当的工作簿:
Dim MacroSheet as Workbook
Set MacroSheet = ThisWorkbook
Dim ScanFile as Workbook
Set ScanFile = Workbooks.Open("PATH TO YOUR FILE")
Then you can use it like this: 然后,您可以像这样使用它:
MacroSheet.Columns("G").Copy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.