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
trusxlsolBusiness Systems AnalystAsked:
Who is Participating?
 
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
 
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
 
trusxlsolBusiness 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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
 
trusxlsolBusiness 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
 
trusxlsolBusiness Systems AnalystAuthor Commented:
Thank you it worked well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.