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:
Excel 2010
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: