Run-time error '9': subscript out of range

marcus.brown

Board Regular
Joined
Jun 23, 2008
Messages
61
Hi,

I'm trying to create a macro for a colleague, to run an advanced filter on one of their sheets.

I've cobbled together the code below, but if the workbook isn't open, I get an error (Run-time error '9': Subscript out of range). When i click on debug, it highlights the code below which i've put in bold.

Can you please help?

Thanks!

Dim wb As Workbook
Dim AlreadyOpen As Boolean

AlreadyOpen = False
For Each wb In Workbooks
If wb.Name = "Monitor Record Sheet.xlsm" Then
AlreadyOpen = True
Exit For
End If
Next wb
If AlreadyOpen = False Then
Workbooks.Open Filename:="U:\Record Sheet.xlsm", ReadOnly:=True
End If

Workbooks("Record Sheet.xlsm").Sheets("ALL blue monitoring data"). _
Columns("D:AE").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Sheets( _
"Sheet2").Range("A1:A2"), CopyToRange:=Range("A1"), Unique:=False
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Maybe your problem is that you haven't qualified Sheet2 with its workbook or Range("A1") with its worksheet. Try:

Code:
With Workbooks("Record Sheet.xlsm")
    .Sheets("ALL blue monitoring data").Columns("D:AE").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Sheets("Sheet2").Range("A1:A2"), CopyToRange:=.Sheets("ALL blue monitoring data").Range("A1"), Unique:=False
End With
 
Upvote 0
no, it didn't work. I get the same error.

Just to clarify, i'm filtering from 1 workbook (Record Sheet) and pasting in another, my criteria is in the same workbook as to where i'm pasting it to - just in a different sheet.
 
Upvote 0
Not sure I'm reading this right, but you open and test for:

"Monitor Record Sheet.xlsm"

Yet your last line of macro code uses:

"Record Sheet.xlsm"

shouldn't these be spelled the same?
 
Upvote 0
Well Excel can't find something. To see what it is try:

Code:
MsgBox Workbooks("Record Sheet.xlsm").Name
MsgBox Workbooks("Record Sheet.xlsm").Sheets("ALL blue monitoring data").Name
MsgBox Sheets("Sheet2").Name
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

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