Excel

Display the Sheet Name of a Single-Cell Sheet Reference

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Zack Barresse

Description:

You may sometimes have the desire to always show the sheet name of a cell that is linked to another sheet. This User Defined Function (UDF) is an easy way to do that. 

Discussion:

If you have multiple cells referencing another sheet, e.g. =Sheet1!A1, and want to see the sheet name that particular formula is referencing, the old way was to format the cell as text or precede the formula with an apostrophe to display it as text. Now you can keep your formula and just add this UDF to view it. This does NOT go well in complex functions or anything other than single cell references. 

Code:

instructions for use

			

Option Explicit Function GetName(Target As Range) 'Skipping cells that do not contain formulas If Target.HasFormula = True Then 'Will error out usually if the formula is a single cell reference w/o a sheet name referenced On Error GoTo endMe GetName = Mid(Target.Formula, 2, WorksheetFunction.Find("!", Target.Formula) - 2) End If Exit Function endMe: 'Our error handling GetName = "Invalid" End Function

How to use:

  1. Copy the code above.
  2. From within Excel hit Alt + F11 to open the Visual Basic Editor (VBE).
  3. From the VBE menu choose Insert -> Module.
  4. Paste code in blank window at right.
  5. Press Alt + Q to quite the VBE and return to Excel.
 

Test the code:

  1. Use the function like you would any other Excel function:. =GetName(A1)
 

Sample File:

GetNameEx.zip 7.47KB 

Approved by mdmackillop


This entry has been viewed 117 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express