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. 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.
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. 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? I tried working with Thisworkbook and Activeworkbook to no avail. I am not a programmer so most of this has been recorded with me tweaking inside the VBA editor.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.