Subscript out of range VBA

tHE fLY

New Member
Joined
Jul 8, 2014
Messages
31
Hi All,

I am getting he Subscript out of range error when trying to run this macro. I have the macro on a macro workbook and I am referencing another spreadsheet. any help would be appreciated.

Sub Dater()
Static Count As Integer
Dim Wb As Workbook
Dim Ws As Worksheet
Dim FileStr As String
Set Wb = Workbooks("Pending Approval by Responsible User")
Set Ws = Worksheets("Pending Approval by Responsible") <----- getting error here.. Only get error hen am using for control to call Macro
With Ws.Range("A3").CurrentRegion
Range("A3").Select
Selection.AutoFilter
.AutoFilter Field:=7, Criteria1:=">=" & CLng(Date), Operator:=xlAnd, Criteria2:="<=" & CLng(Date + 10)
End With
ActiveWorkbook.Worksheets("Pending Approval by Responsible").AutoFilter.Sort. _
SortFields.Add Key:=Range("G3:G1000"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Pending Approval by Responsible").AutoFilter. _
Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Code:
Set Ws = ActiveWorkbook.Sheets("Pending Approval by Responsible")

or

Code:
Set Ws = Wb.Sheets("Pending Approval by Responsible")

depending on what you're trying to accomplish.
 
Upvote 0
the fly,
You are getting this error, because the workbook the macro is running from cannot find the worksheet "Pending Approval by Responsible". I am assuming your worksheet "Pending Approval by Responsible" is located in the workbook "Pending Approval by Responsible User". If this is the case, try Set Ws = Wb.Worksheets("Pending Approval by Responsible").

This will only work if the instance of excel that the macro is running from also has an open copy of "Pending Approval by Responsible User". If that is not going to be the case, you will have to open that workbook with set wb = application.workbooks.open(path)

Hope this helps,

CN.
 
Upvote 0
Thanks.. all of you were right on! I am learning VBA so sorry if that seems like a softball question. =)
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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