Run-Time Error "9": Script out of Range Workbook_Open Workbooks.activate

marionffavp

New Member
Joined
Nov 11, 2016
Messages
3
Hi Everyone,

What I am trying to accomplish is to have an Excel 2013 file that will open and automatically do the following:

1.) Open a different Excel workbook ("Workbook2") from a SharePoint Site as read only.
2.) Apply a filter on two different columns for the ("Workbook2") Excel workbook.
3.) Copy the values from 6 different columns into the next usable row of the originating workbook ("Workbook1").
4.) Remove any duplicates that are found in the first workbook("Workbook1").
5.) close the ("Workbook2") file.

The problem I am having is that ("Workbook2") is not getting the filter applied to it, but ("Workbook1") is and that is not what needs to happen. The really funny thing is that it worked perfectly when I stepped through the code using F8. That's what I get for not REALLY testing it out with a final run.


below is the code:

Private Sub Workbook_Open()
'
'
Dim CurWorkbook As Workbook
Dim Workbook1EndRow As Long
Dim Workbook1StartCopyRow As Long
Dim Workbook2EndRow As Long
Dim Workbook1AfterPasteRow As Long
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Set CurWorkbook = ActiveWorkbook

Workbooks.Open "SharePoint Site Link", ReadOnly:=True
Workbooks("Workbook2").Sheets("Sheet1").Activate 'This is where the error occurs.

With ActiveSheet
.AutoFilterMode = False
.UsedRange.AutoFilter
.UsedRange.AutoFilter field:=37, Criteria1:=xlFilterYesterday, Operator:=xlFilterDynamic
.UsedRange.AutoFilter field:=29, Criteria1:="*criteria*"
End With


Workbooks("Workbook1").Sheets("Sheet1").Activate
Workbook1EndRow = Cells(Rows.Count, 1).End(xlUp).Row
Workbook1StartCopyRow = Cells(Rows.Count, 1).End(xlUp).Row + 1


'Copy and paste the values starting from the starting cell number.
'Get the notification number
Range("AD2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Workbooks("Workbook1").Activate
ActiveSheet.Paste Destination:=Cells(Workbook1StartCopyRow, 1)

Application.CutCopyMode = False

'Get the Created on Date
Windows("Workbook2").Activate
Range("AK2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Workbooks("Workbook1").Activate
ActiveSheet.Paste Destination:=Cells(Workbook1StartCopyRow, 2)

Application.CutCopyMode = False

'Get the Serial Number

Windows("Workbook2").Activate
Range("AB2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Workbooks("Workbook1").Activate
ActiveSheet.Paste Destination:=Cells(Workbook1StartCopyRow, 3)

Application.CutCopyMode = False

'Get the Description
Windows("Workbook2").Activate
Range("AC2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Workbooks("Workbook1").Activate
ActiveSheet.Paste Destination:=Cells(Workbook1StartCopyRow, 4)

Application.CutCopyMode = False
'Get the Long Text.
Windows("Workbook2").Activate
Range("K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Workbooks("Workbook1").Activate
ActiveSheet.Paste Destination:=Cells(Workbook1StartCopyRow, 5)

Application.CutCopyMode = False

'Get the Activity Text
Windows("Workbook1").Activate
Range("L2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Workbooks("Workbook1").Activate
ActiveSheet.Paste Destination:=Cells(Workbook1StartCopyRow, 6)

Application.CutCopyMode = False

'Find the duplicate range to remove duplicates from.
Workbook1AfterPasteRow = Cells(Rows.Count, 1).End(xlUp).Row

'remove duplicates.

ActiveSheet.Range("$A:I").RemoveDuplicates Columns:=1, Header:=xlYes


Workbooks("workbook2").Close

Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
The error message simply means that you don't have an open workbook called "Workbook2", e.g. "Workbook2.xls" or "Workbook2.xlsx".

I am guessing that you are trying to do something like this:

Code:
Dim Workbook2 As Workbook

Set Workbook2 = Workbooks.Open(Filename:="YourFilenameStringGoesHere", ReadOnly:=True)
 
Upvote 0
Hi StevenCrump,

Thank you for your speedy response! Your code helped me. At this time, I am running across other issues. Like how to make Excel to check if the day of the week is a Monday and then filter on data from Friday, Saturday and Sunday.

Happy Coding!

Thanks again
 
Upvote 0
Thanks again StevenCrump! You were most helpful. I did not even think about defining the workbook in Excel VBA. I thought I had coded things before and it was not required or perhaps I did and I'm getting old and forgetting things.
I changed my solution to be an outside Excel file with a command button to click to make the events occur. Below is my solution:
Sub CopyPasteFilterDates()<o:p></o:p>
Dim File1EndRow As Long<o:p></o:p>
Dim File2StartCopyRow As Long<o:p></o:p>
Dim File2EndRow As Long<o:p></o:p>
Dim File2AfterPasteRow As Long<o:p></o:p>
Dim File2 As Workbook<o:p></o:p>
Dim TodaysDate As Date<o:p></o:p>
Dim StartDate As Long<o:p></o:p>
Dim EndDate As Long<o:p></o:p>
<o:p> </o:p>
TodaysDate = Date<o:p></o:p>
StartDate = Date<o:p></o:p>
EndDate = Date - 3<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Application.DisplayAlerts = False<o:p></o:p>
Application.ScreenUpdating = False<o:p></o:p>
Application.DisplayStatusBar = False<o:p></o:p>
Application.EnableEvents = False<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
'Apply a filter that will only find items if they have"*items*"anywhere in the Description and yesterday's date.<o:p></o:p>
'Open the File1 spreadsheet.<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Set File1Data = Workbooks.Open("SharePoint Site Link/File1.xlsx",ReadOnly:=True)<o:p></o:p>
<o:p> </o:p>
Workbooks.Open"SharePoint Site Link/File1.xlsx"<o:p></o:p>
Workbooks("File2.xlsx").Activate<o:p></o:p>
Workbooks.CheckOut"File2.xlsx"<o:p></o:p>
<o:p></o:p>
With ActiveSheet<o:p></o:p>
.AutoFilterMode =False<o:p></o:p>
End With<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Workbooks("File1.xlsx").Activate<o:p></o:p>
<o:p> </o:p>
<o:p></o:p>
If WorksheetFunction.Weekday(TodaysDate) <> 2 Then<o:p></o:p>
With ActiveSheet<o:p></o:p>
.AutoFilterMode = False<o:p></o:p>
.UsedRange.AutoFilter<o:p></o:p>
.UsedRange.AutoFilter field:=37, Criteria1:=xlFilterYesterday,Operator:=xlFilterDynamic<o:p></o:p>
.UsedRange.AutoFilter field:=29, Criteria1:="**"<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
Else<o:p></o:p>
With ActiveSheet<o:p></o:p>
.AutoFilterMode = False<o:p></o:p>
.UsedRange.AutoFilter<o:p></o:p>
.UsedRange.AutoFilter field:=37, Criteria1:=">=" &EndDate, Operator:=xlAnd, Criteria2:="<=" & StartDate<o:p></o:p>
.UsedRange.AutoFilter field:=29,Criteria1:="**"<o:p></o:p>
End With<o:p></o:p>
End If<o:p></o:p>
<o:p> </o:p>
<o:p></o:p>
<o:p> </o:p>
'Find the end of the row in File2 before doing anythingelse.<o:p></o:p>
Workbooks("File2.xlsx").Sheets("Sheet1").Activate 'Go to “Sheet1” worksheet of File2.<o:p></o:p>
File2EndRow =Cells(Rows.Count, 1).End(xlUp).Row 'Search File2 to find the last unused row.<o:p></o:p>
File2StartCopyRow= Cells(Rows.Count, 1).End(xlUp).Row + 1 'Add one to bring the line down onerow.<o:p></o:p>
<o:p> </o:p>
<o:p></o:p>
' ActiveSheet.Range("$A$1:$BE$7128").AutoFilter field:=37,Criteria1:= _<o:p></o:p>
' xlFilterYesterday, Operator:=xlFilterDynamic<o:p></o:p>
'<o:p></o:p>
' ActiveSheet.Range("$A$1:$BE$7128").AutoFilter field:=29,Criteria1:="*items*"<o:p></o:p>
'<o:p></o:p>
'Copy and paste the values starting from the starting cellnumber.<o:p></o:p>
'Get the data startingin cell AD2 to gather all the filtered values only.<o:p></o:p>
Windows("File1.xlsx").Activate<o:p></o:p>
Range("AD2").Select<o:p></o:p>
Range(Selection,Selection.End(xlDown)).Select<o:p></o:p>
Selection.Copy<o:p></o:p>
<o:p></o:p>
Workbooks("File2.xlsx").Activate<o:p></o:p>
ActiveSheet.PasteDestination:=Cells(File2StartCopyRow, 1) ‘ Start in the first usable row and incolumn A.<o:p></o:p>
<o:p></o:p>
Application.CutCopyMode = False<o:p></o:p>
<o:p></o:p>
'Get the data startingin cell AK2 to gather all the filtered values only.<o:p></o:p>
Windows("File1.xlsx").Activate<o:p></o:p>
Range("AK2").Select<o:p></o:p>
Range(Selection,Selection.End(xlDown)).Select<o:p></o:p>
Selection.Copy<o:p></o:p>
<o:p></o:p>
Workbooks("File2.xlsx").Activate<o:p></o:p>
ActiveSheet.PasteDestination:=Cells(File2StartCopyRow, 2) ‘ Start in the first usable row and incolumn B.<o:p></o:p>
<o:p></o:p>
Application.CutCopyMode = False<o:p></o:p>
<o:p></o:p>
'Get the data startingin cell AB2 to gather all the filtered values only.<o:p></o:p>
<o:p></o:p>
Windows("File1.xlsx").Activate<o:p></o:p>
Range("AB2").Select<o:p></o:p>
Range(Selection,Selection.End(xlDown)).Select<o:p></o:p>
Selection.Copy<o:p></o:p>
<o:p></o:p>
Workbooks("File2.xlsx").Activate<o:p></o:p>
ActiveSheet.PasteDestination:=Cells(File2StartCopyRow, 3) ‘ Start in the first usable row and incolumn C.<o:p></o:p>
<o:p></o:p>
Application.CutCopyMode = False<o:p></o:p>
<o:p></o:p>
'Get the data startingin cell BB2 to gather all the filtered values only.<o:p></o:p>
Windows("File1.xlsx").Activate<o:p></o:p>
Range("BB2").Select<o:p></o:p>
Range(Selection,Selection.End(xlDown)).Select<o:p></o:p>
Selection.Copy<o:p></o:p>
<o:p></o:p>
Workbooks("File2.xlsx").Activate<o:p></o:p>
ActiveSheet.PasteDestination:=Cells(File2StartCopyRow, 4) ‘ Start in the first usable row and incolumn D.<o:p></o:p>
<o:p></o:p>
Application.CutCopyMode = False<o:p></o:p>
<o:p></o:p>
'Get the data startingin cell AC2 to gather all the filtered values only.<o:p></o:p>
Windows("File1.xlsx").Activate<o:p></o:p>
Range("AC2").Select<o:p></o:p>
Range(Selection,Selection.End(xlDown)).Select<o:p></o:p>
Selection.Copy<o:p></o:p>
<o:p></o:p>
Workbooks("File2.xlsx").Activate<o:p></o:p>
ActiveSheet.PasteDestination:=Cells(File2StartCopyRow, 5) ‘ Start in the first usable row and incolumn E.<o:p></o:p>
<o:p></o:p>
Application.CutCopyMode = False<o:p></o:p>
<o:p> </o:p>
'Get the data startingin cell K2 to gather all the filtered values only.<o:p></o:p>
Windows("File1.xlsx").Activate<o:p></o:p>
Range("K2").Select<o:p></o:p>
Range(Selection,Selection.End(xlDown)).Select<o:p></o:p>
Selection.Copy<o:p></o:p>
<o:p></o:p>
Workbooks("File2.xlsx").Activate<o:p></o:p>
ActiveSheet.PasteDestination:=Cells(File2StartCopyRow, 6) ‘ Start in the first usable row and incolumn F.<o:p></o:p>
<o:p></o:p>
Application.CutCopyMode = False<o:p></o:p>
<o:p></o:p>
'Get the data startingin cell L2 to gather all the filtered values only<o:p></o:p>
Windows("File1.xlsx").Activate<o:p></o:p>
Range("L2").Select<o:p></o:p>
Range(Selection,Selection.End(xlDown)).Select<o:p></o:p>
Selection.Copy<o:p></o:p>
<o:p></o:p>
Workbooks("File2.xlsx").Activate<o:p></o:p>
ActiveSheet.PasteDestination:=Cells(File2StartCopyRow, 7) ‘ Start in the first usable row and incolumn G.<o:p></o:p>
<o:p></o:p>
Application.CutCopyMode = False<o:p></o:p>
<o:p></o:p>
'Find theduplicate range to remove duplicates from.<o:p></o:p>
AfterPasteRow =Cells(Rows.Count, 1).End(xlUp).Row<o:p></o:p>
<o:p></o:p>
'removeduplicates.<o:p></o:p>
ActiveSheet.Range("$A:I").RemoveDuplicates Columns:=1,Header:=xlYes<o:p></o:p>
<o:p></o:p>
'Close File1.<o:p></o:p>
Workbooks("File1.xlsx").Close<o:p></o:p>
Windows("File2.xlsx").Activate<o:p></o:p>
Workbooks("File2.xlsx").CheckIn<o:p></o:p>
<o:p> </o:p>
Application.DisplayAlerts = True<o:p></o:p>
Application.ScreenUpdating = True<o:p></o:p>
Application.DisplayStatusBar = True<o:p></o:p>
Application.EnableEvents = True<o:p></o:p>
End Sub

Merry Christmas and Happy New Year!!! Very Happy Coding!

 
Upvote 0

Forum statistics

Threads
1,203,043
Messages
6,053,181
Members
444,643
Latest member
Shipwreck818

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top