Reference a Defined Name in VBA in an Excel AddIn

Posted on 2015-01-08
Last Modified: 2015-01-08
I have added a Defined Name called "ConnectionString" on "Sheet1" in an Excel AddIn I have written.  What is the proper way to reference that Defined Name within the VBA of the AddIn itself?

ActiveWorkbook.Range("ConnectionString").Value   and   ThisWorkBook.Range("ConnectionString").Value
are not correct because they will look for the defined name in the file the user is working with and not the AddIn workbook.   I want to reference the name inside the VBA of the AddIn.

Question by:Jerry Paladino
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 29

Expert Comment

ID: 40539122
when you reach Sheet1 in VBA of the Addin
Dim WS as worksheet
set WS = sheets("Sheet1")
WS.Range("ConnectionString").Value  .... should reference what you want.

LVL 27

Expert Comment

by:Glenn Ray
ID: 40539140
I believe you'll need to activate the Add-In file in order to retrieve the defined name.  For example:

Sub ProcessIt()
    Dim strActiveWB as String

    strActiveWB = ActiveWorkbook.Name

    Application.Workbooks("addinname").Activate 'replace with actual filename in quotes including extension - xlam
    strCS = Sheets("Sheet1").Range("ConnectionString").Value


End Sub

Open in new window

LVL 29

Expert Comment

ID: 40539141
or I should rather say


LVL 47

Accepted Solution

Wayne Taylor (webtubbs) earned 500 total points
ID: 40539327
You can get the value of a range in a worksheet of the Add-In in the same way you would any other workbook....

LVL 16

Author Closing Comment

by:Jerry Paladino
ID: 40539412

Thank you...   Yes, this works well for what I am trying to accomplish.   A nice plus is that since the AddIn is already loaded I can reference the AddInName without hard coding the path or using Application.UserLibraryPath to get the path.  Much appreciated.

gowflow & Glenn - thank you both for your suggestions.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question