Runtime error 9 - subscript out of range

Afro_Cookie

Board Regular
Joined
Mar 17, 2020
Messages
103
Office Version
  1. 365
Platform
  1. Windows
We use a macro daily to update some of our production files and when we run the macro, it only works on some computers. We've been using the same computer to run this and other queries for over a year and until this month, we have not had any issues. I can't determine why one computer would be able to run the macro while another cannot.

Below is the code and when I run the code line by line, it stops at tbl.Copy Workbooks("report").Sheets("Sort").Range("A1"). Any help possible would be greatly appreciated.
VBA Code:
Sub gather_information()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Workbooks.Open "source file"

Dim tbl As Range
Set tbl = Sheets("main").AutoFilter.Range
Set tbl = tbl.Resize(tbl.Rows.Count - 1)
Set tbl = tbl.Offset(1)

Sheets("main").Select

ActiveSheet.Range("$A$1:$J$1").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, Date)
tbl.Copy Workbooks("report").Sheets("Sort").Range("A1")

Workbooks("source file").Close SaveChanges:=False

End Sub

1652187578160.png
 

Attachments

  • 1652187673808.png
    1652187673808.png
    4.7 KB · Views: 3

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
My first guess is that it is a Windows Explorer setting issue on the other computer and that this thread might help you.
 
Upvote 0
You need to add the file extension to the name of the workbook.
 
Upvote 0
Solution
You need to add the file extension to the name of the workbook.
I changed the name of the file in my post as I can't share the actual path. This is what it roughly looks like "P:\folder\sub folder\sub sub folder\file.xls"

As for the file extensions, I don't have that option for some reason. This is what my 'view' tab looks like
1652188841646.png
 
Upvote 0
It's nothing to do with Excel, you need to add the extenstion to the workbook name in your code
VBA Code:
Workbooks("report.xlsx").Sheets("Sort").Range("A1")
 
Upvote 0
It's nothing to do with Excel, you need to add the extenstion to the workbook name in your code
VBA Code:
Workbooks("report.xlsx").Sheets("Sort").Range("A1")
I added the extension to the file path and it appears to have worked. I'm curious though as to why this would affect 1 computer and not another when the same code is being run.
 
Upvote 0
As Peter said, it's down to the Windows Explorer settings. If you have "Hide known file extensions" checked, then you do not need the extension, other you do.
 
Upvote 0

Forum statistics

Threads
1,214,994
Messages
6,122,633
Members
449,092
Latest member
bsb1122

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