Subscript out of range (for some users)

bwturner1

New Member
Joined
May 30, 2019
Messages
3
What is the most likely cause for this bit of code to work for one user but not another? It throws a subscript out of range error at the highlighted line when users other than me run it. This issue has vexed me for over 2 weeks now so any assistance is greatly appreciated, in advance. I will gladly suffer any embarrassment if I'm doing something fundamentally wrong.

Code:
Sub MyTestSub()

Dim MyPathFileName As String, MyFileName As String, MyWBName As String, MyContents As String


MyPathFileName = Application.GetOpenFilename("Excel Files (*.xlsx), * .xlsx")
MyFileName = Right(MyPathFileName, Len(MyPathFileName) - InStrRev(MyPathFileName, "\"))
MyWBName = Replace(MyFileName, ".xlsx", "")
Workbooks.Open Filename:=MyPathFileName
[COLOR=#ff0000]Workbooks(MyWBName).Activate[/COLOR]
MyContents = Workbooks(MyWBName).Sheets("Sheet1").Range("B3").Value


MsgBox "Cell B3 contains " + MyContents


End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Why are you removing the file extension?

That could be what's causing the error you describe.

The reason why certain users could be down to their Windows settings, specifically if they are set to show/hide known file extensions.

You can probably avoid the problem by not removing extension.
 
Upvote 0
You can probably avoid the problem by not removing extension.

Yup! Goes to show my elementary understanding of VBA file operations. Interestingly, I do not show file extensions in my Windows environment but the other user does. Instead of assuming everyone has the same Windows setting as me, your solution is more elegant and independent of personal preferences. Thanks!

wt
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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