Macro shortcut behaviour is different from Alt-F8 Run Macro

untrue

New Member
Joined
Jul 21, 2014
Messages
2
Hi,

I have a macro that looks into the cell formula for the reference to another excel file name and sheet name and opens it/activates the sheet name. When I run the macro via Alt-F8 run macros, it works as expected, but when I assign a Shortcut it does open the file but do not activate the sheet.
The macro is below:
Code:
Sub OpenFile()
Dim wbk As Excel.Workbook, Sh As Excel.Worksheet
Dim FilePath, FileName, SheetName, Formula As String
Formula = Replace(ActiveCell.Formula, "'", "")
On Error GoTo errexit
FileName = Mid(Formula, InStr(1, Formula, "[") + 1, InStr(1, Formula, "]") - 1 - InStr(1, Formula, "["))
SheetName = Mid(Formula, InStr(1, Formula, "]") + 1, InStr(1, Formula, "!$") - InStr(1, Formula, "]") - 1)

If InStr(1, Formula, ":\") > 0 Then
FilePath = Mid(Formula, InStr(1, Formula, ":\") - 1, InStr(1, Formula, "[") - InStr(1, Formula, ":\") + 1)
Workbooks.Open (FilePath & FileName)
Workbooks(FileName).Sheets(SheetName).Activate
Else
FilePath = ""
Workbooks(FileName).Activate
Workbooks(FileName).Sheets(SheetName).Activate
End If

errexit:
'MsgBox "Something is wrong", vbCritical, "Macro Error"
End Sub

Excel 2010
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If your shortcut key combination involves the Shift key, try using one that doesn't. There is a bug that will cause the code to stop immediately after it executes the Workbooks.Open line.
 
Upvote 0
If your shortcut key combination involves the Shift key, try using one that doesn't. There is a bug that will cause the code to stop immediately after it executes the Workbooks.Open line.

Exactly the case. Thanks!
 
Upvote 0
Glad to help. Welcome to the forum, by the way. :)
 
Upvote 0

Forum statistics

Threads
1,216,730
Messages
6,132,388
Members
449,725
Latest member
Enero1

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