Skip to main content

Hyperlinks in VBA


I have programmed in MS Excel using VBA (Visual Basic for Applications) fairly regularly over the past 25+ years. This makes me experienced but not necessarily expert. Frustration over the past several days seems to have demonstrated the lack of expertise, particularly in handling hyperlinks between Excel workbooks.

The project involved building a hyperlink bridge between specific pages/worksheets within two workbooks. The environment wasn’t too difficult since both workbooks would be housed in the same directory. However, the two workbooks would have a parent / child relationship.

The code was designed to build a new control worksheet in the parent workbook and hyperlink it to a specific worksheet and cell range in a child workbook book. The child workbook would be generated entirely in code before being saved.

This didn’t sound too difficult and it wasn’t; until it came to trying to create the hyperlinks between pages of the different workbooks.

The first thing to note is that hyperlink functionality is not native to VBA (for Excel). It is effectively a scripting function for HTML. Also, although the Microsoft Office software elements (e.g. Word, Excel and Access) use identical scripting functions, the SubAddress nomenclature elements vary according to the software used. For example, the SubAddress for Word employs “bookmarks” whereas in Excel we use cell addresses or “names”. Furthermore, the structure of a Word file is fundamentally different from an Excel file. Word files have a single pot, whereas Excel files have multiple worksheets which act as separate pots.

These differences, although apparently small and insignificant, make programming hyperlinks in Excel’s version of VBA frustratingly difficult.

Programming a Hyperlink in Excel

In Excel the hyperlink is programmed for a worksheet or a specific range, but not the workbook. See the example “ThisControlSht.Hyperlinks.Add”, where ThisControlSht is a worksheet variable generated earlier in the program.

The Hyperlinks.Add function has several parameters – Anchor, Address, SubAddress, ScreenTip and TextToDisplay. Each parameter has its own elements which may not be obvious at first glance.

Anchor must be a range. It is the cell in which you wish to place the hyperlink. So either Anchor:=ThisControlSht.Range("MapControlsheet") or Anchor:=ThisControlSht.Range(B5) will work. In this case the cell B5 was named “MapControlSheet”. However, Anchor”=MapControlSheet” does NOT work even though you may appear to be in the correct environment.

Address is usually a website URL but in our case we are linking to another Excel file. For security I employ the full path of the file address and name (including suffix of the file).

SubAddress allows you to specify both the worksheet and particular range of the target. In the Excel environment “names” can be set for the Workbook or specific to a particular Worksheet, but you must ignore that distinction when specifying a SubAddress. You need to specify both the Worksheet and the range. The range can be in the form of a cell address like B5 or a named range like SubAddress:="=DataList_and_MappingIndex!MapListMappingIndex". Note that the sheet name and the range are separated by “!”.

TextToDisplay is an optional parameter and is usually only employed when the cell is empty. However, in the context of this particular program the workbooks / worksheets were being generated in code and therefore all cells were empty unless you had already coded some value to be posted in. In this case I hadn’t. In testing, the program fell over until I had placed some suitable value in the  TextToDisplay.

I would not have got even this far without Excel’s macro recorder. That process generated this code.

Sub Macro2()
'
' Macro2 Macro
'

'
    Windows("My_Test_Book.xlsx").Activate
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
        "My_Test_Book_MapBook.xlsx", SubAddress:="DataList_and_MappingIndex!A1", _
        TextToDisplay:="My_Test_Book_MapBook.xlsx#DataList_and_MappingIndex!A1"
End Sub


Comments

Popular posts from this blog

Cell Maps - Critical Comments

Cell maps are intended as tools for reviewing spreadsheets. If you spot an error or an inconsistency in the cell map this should be recorded and, if practicable, corrected. The cell mapping software provides a method for recording a reviewer's comments. All comments are linked to a specific map (or data table), The comments for a workbook under review are collated in a single worksheet. In this case, there's just one comment. The comment above refers to the area highlighted towards the bottom of the map below and are hyperlinked.to the specific area of the map through the column headed 'Map/Source Link'. The top left cell the source range provides a link back to the 'Comments. worksheet'. The yellow cells suggest that users should be entering information for collection by a formula, but which one.  At the this point I went back to the orginal worksheet and traced the dependents for some of these 'so called' user entry cells - also coloured yellow as it ...

A Revised Cell Map!

  In the Blog ' What is a Cell Map? ', I included an image of a Cell Map. On reflection the presentation was muddled. A simpler and clearer design seemed worthwhile. The result below is more clearly rectangular, with the headings for the map, the formula table and the names/validation table matched along the same row. The validation table is new and highlights where user inputs are limited by validation and the nature of the validation criteria. The revised software is available at ACBA-Mapping . In addition, although software is intended to be intuitive and self explanatory, it rarely is. I have created a user group at    MSExcelCellMapping@groups.io   . You are welcome to join and ask questions about the software. https://drive.google.com/file/d/1w3tHk1l-G9LjmCsOqMBkJpE9kaSj0iX_/view?usp=sharing Spreadsheet errors and inconsistensies is a subject that is of concern by professional developers as well as amateurs. The European Spreadsheet Risk Interest Group (EuSpRIG...

Loading an Excel Add-In

  Microsoft offers a range of Add-Ins and other extensions to Excel’s normal functionality. These are accessed most easily from the ‘Developer’ ribbon. The Developer Tab Clicking the Add-Ins icon, offers the user a list of all Add-Ins currently available to your Excel program. The problem comes when your particular program isn’t listed there. Even when you know where the Add-In file is and double click it, the functionality is still not activated. According to SuperUser.com , the problem results from security patch in KB31152, released in July 2016. Apparently, Microsoft software engineers have said: "With this update, we changed the behaviour of Excel so that it will not load certain file types (including .xlam) when they are untrusted. The easiest workaround is to find the add-in that is causing you trouble, right-clicking on it in Windows Explorer, and checking Unblock" However, SuperUser.com recommend placing the add-in in a Trusted Location (in Excel, go to File ...