File Open Event Macro

Goal:
On file open Loader.xlsm file should open the LoaderStatus.xlsx file and copy the range A which is holding timestamp information in Loader.xlsm file then paste and close the LoaderStatus.xlsx file.  I placed the macro LoadStatus in "Thisworkbook" section. There are other call macros too under Private Sub Workbook_Open() sub located in "This Workbook" of the Loader.xlsm file but LoadStatus macro is the focus of the problem so you can deactivate them when working on this issue.

Detail:
When I run below macro (FileMoveManual) located in Loader.xlsm  file it works just fine, but once the same exact code (for the LoadStatus sub) is placed "In this workbook" section of the same file so it can run on Workbook open event it produces runtime error 9 - Subscript Out of Range once I open the workbook.

The problem is with the LoadStatus sub located in Private Sub Workbook_Open().  I tried to isolate the issue and the line with the problem is "Windows("LoaderStatus.xlsx").Activate".  On file open event macro opens the LoaderStatus.xlsx file then gets back to Loader.xlsm file to copy column A then needs to get back to LoaderStatus.xlsx file to paste the range but it fails right there when it needs to activate the LoaderStatus.xlsx file to paste the range.  But it is not having any problems of doing this if it is run from the macro manually instead doing on file open.

FileMoveManual macro that works fine when running manually on Loader.xlsm file:
This macro is assigned to the button "MIAC FIRM Report File Copy" on Sheet2 of the Loader.xlsm file.

Sub FileMoveManual()
Call Timestamp
Call Copy_One_File
Call LoadStatus1
'Call Move_Rename_One_File
MsgBox "MIAC - FIRM Report File Copied Successfully", vbInformation, "Done!"
End Sub

Sub LoadStatus1()
Workbooks.Open filename:= _
    "C:\Users\rerden\Desktop\LoaderStatus.xlsx"
    Windows("Loader.xlsm").Activate
    Sheets("Sheet2").Select
    Columns("A:A").Select
    Selection.Copy
    Windows("LoaderStatus.xlsx").Activate
    Sheets("Sheet1").Select
    Range("A1").Select
    ActiveSheet.Paste
    ActiveWorkbook.Save
    ActiveWorkbook.Close

End Sub

Code Block (LoadStatus) Placed in "In This Workbook" of Loader.xlsm to run on file open via macro call and producing error:

Private Sub Workbook_Open()

Call Timestamp
Call LoadStatus
Call Copy_One_File
'Call Move_Rename_One_File


'MsgBox "MIAC - FIRM Report File Copied Successfully", vbInformation, "Done!"

 ActiveWorkbook.Save
 'ActiveWorkbook.Close
 
End Sub


Sub LoadStatus()
Workbooks.Open filename:= _
    "C:\Users\rerden\Desktop\LoaderStatus.xlsx"
    Windows("Loader.xlsm").Activate
    Sheets("Sheet2").Select
    Columns("A:A").Select
    Selection.Copy
    Windows("LoaderStatus.xlsx").Activate - this line is not running on file open event
    Sheets("Sheet1").Select
    Range("A1").Select
    ActiveSheet.Paste
    ActiveWorkbook.Save
    ActiveWorkbook.Close

End Sub

Thank you for help!
Loader.xlsm
LoaderStatus.xlsx
Ray ErdenBusiness Systems AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
Can you use this code...

This will do what you are looking for...

Sub LoadStatus()
Dim wb As Workbook

Set wb = Workbooks.Open("C:\Users\rerden\Desktop\LoaderStatus.xlsx")
    Windows("Loader.xlsm").Activate
    Sheets("Sheet2").Select
    Columns("A:A").Select
    Selection.Copy
    wb.Activate
    Sheets("Sheet1").Select
    Range("A1").Select
    ActiveSheet.Paste
    ActiveWorkbook.Save
    ActiveWorkbook.Close

End Sub

Open in new window


Saurabh...
0
Ray ErdenBusiness Systems AnalystAuthor Commented:
I ran your code as displayed above within LoadStatus() sub but it failed.  The error line is Sheets("Sheet1").Select.  Tried the same block as running a macro manually instead of doing a file open then it works.  Any thoughts?
0
Saurabh Singh TeotiaCommented:
This works for me without any problem..

Sub LoadStatus()
Dim wb As Workbook

Set wb = Workbooks.Open("C:\Users\rerden\Desktop\LoaderStatus.xlsx")
    Windows("Loader.xlsm").Activate
    Sheets("Sheet2").Select
    Columns("A:A").Select
    Selection.Copy
 wb.Activate
   wb.Sheets("Sheet1").Select
    Range("A1").Select
    ActiveSheet.Paste
    ActiveWorkbook.Save
    ActiveWorkbook.Close

End Sub

Open in new window



Saurabh...
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Ray ErdenBusiness Systems AnalystAuthor Commented:
Here is the scoop, this time it did run without any issues, no error messages.  But copied range from the Loader.xlsm file did not get pasted into the LoaderStatus.xlsx file.  Again I did run this code also as a macro instead of letting it running on file open then the timestamp range gets pasted this time.
0
Saurabh Singh TeotiaCommented:
I just re-wrote your code this will do the same thing what you are looking for..

Sub LoadStatus()
    Dim wb As Workbook

    Set wb = Workbooks.Open("C:\Users\rerden\Desktop\LoaderStatus.xlsx")
    Windows("Loader.xlsm").Activate
    Sheets("Sheet2").Select
    Columns("A:A").Copy wb.Sheets("Sheet1").Range("A1")
    wb.Close (True)

End Sub

Open in new window


Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray ErdenBusiness Systems AnalystAuthor Commented:
Thank you it worked well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.