VBA to run hyperlinks that returns to the original Workbook

gberg

Board Regular
Joined
Jul 16, 2014
Messages
186
Office Version
  1. 365
Platform
  1. Windows
I have three workbooks all in SharePoint folders. The main Workbook has a macro to open the other two Workbooks. The Hyperlinks to the other two Workbooks is in a tab called "Data". I have the following macro that works fine for opening the two other Workbooks but I want it to return to the original workbook and have a code at the end for it (highlighted in red) but it does not run, the last Workbook opened stays active. If I run the last line as a separate macro it works fine. Any ideas?

Sub Open_workbook()

Dim xHyperlink As Hyperlink
Dim WorkRng As Range
On Error Resume Next
'Set Range
Sheets("Data").Activate
Range("Data_Weekly_Spreadsheet:Data_Job_Log").Select
Set WorkRng = Application.Selection
For Each xHyperlink In WorkRng.Hyperlinks
xHyperlink.Follow
Next

Windows("JPR - Monthly Tracker 2022 - w Ext Links.xlsm").Activate

End Sub

Thanks
 

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
So this macro is run from the Main workbook, which is the one you're trying to activate? If so, you could try:

AppActivate ThisWorkbook.Windows(1).Caption

instead of the line in red.

Or if the workbook is the one your code is trying to activate - "JPR - Monthly Tracker 2022 - w Ext Links.xlsm" - and it isn't working, you could try:
AppActivate Workbooks("JPR - Monthly Tracker 2022 - w Ext Links.xlsm").Windows(1).Caption

Alternatively, it might be the case that VBA is moving to quickly for Excel to keep up, in which case you would need to get the code to pause before proceeding to the final line that shifts focus back to the main workbook. Let me know how it goes.
 
Upvote 0
Solution
Thanks for the feedback. It appears to be option 2, the VBA is moving to fast. I put in the pause as you suggested and that worked.
 
Upvote 0

Forum statistics

Threads
1,215,650
Messages
6,126,012
Members
449,280
Latest member
Miahr

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