简体   繁体   English

从Macro工作簿复制和粘贴到多个Excel工作簿

[英]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.

相关问题 将多个工作簿中的工作表复制到一个工作簿中并粘贴到右侧 - Copying worksheets from multiple workbooks into one workbook pasting to the right 从多个工作簿复制到单个工作簿 Excel VBA - Copying from multiple workbooks to single workbook Excel VBA 循环浏览Excel工作簿,从每个工作簿复制一定范围的单元格并粘贴到主工作簿 - Looping through Excel workbooks, copying a range of cells from each and pasting to a master workbook 从各种工作簿中复制工作表并粘贴到其他工作簿中的工作表中 - Copying sheets from various workbooks and pasting into a sheet in a different workbook 将数据从工作簿中的多个工作表复制到单独工作簿中的不同工作表-VBA Excel - Copying Data from Multiple Worksheets in Workbooks to Differing Worksheets in Separate Workbook - VBA Excel VBA代码问题,用于从Excel工作簿中的多个工作表复制和粘贴相同的列 - VBA code issue for copying and pasting same columns from multiple sheets in excel workbook Excel宏可从多个工作簿写入单个工作簿 - Excel macro to write from multiple workbooks to a single 将多个工作簿中的工作表复制到现有工作簿 - VBA - copying sheets from multiple workbooks to existing workbook- VBA 将工作表从多个工作簿复制到另一个工作簿中的现有工作表 - Copying worksheets from multiple workbooks into existing worksheets in a different workbook 将数据从多个标准化工作簿列表复制到活动工作簿 - Copying data from a list of multiple standardised workbooks to the active workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM