Solved

Reference a Defined Name in VBA in an Excel AddIn

Posted on 2015-01-08
5
138 Views
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.

Thanks,
Jerry
0
Comment
Question by:Jerry Paladino
5 Comments
 
LVL 29

Expert Comment

by:gowflow
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.

gowflow
0
 
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

.
.
    Application.Workbooks(strActiveWB).Activate

End Sub

Open in new window


Regards,
-Glenn
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40539141
or I should rather say

ActiveWorkbook.Sheets("Sheet1").Range("ConnectionString").Value

gowflow
0
 
LVL 47

Accepted Solution

by:
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....

    Workbooks("AddInName.xlam").Worksheets("Sheet1").Range("ConnectionString").Value
0
 
LVL 16

Author Closing Comment

by:Jerry Paladino
ID: 40539412
Wayne,

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.

Thanks,
Jerry
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

809 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