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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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