Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 252
Default Referencing across projects

Hi all,

How to reference a macro defined in another workbook?

Clara
--
thank you so much for your help
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Referencing across projects

Clara,

This runs the macro 'Test' in Book1.xls in the root of C

Sub runit()
Set wbTarget = Workbooks.Open("c:\book1.xls")
Application.Run (wbTarget.Name & "!test")
End Sub

You would probably want to include eror checking.

Mike

"clara" wrote:

Hi all,

How to reference a macro defined in another workbook?

Clara
--
thank you so much for your help

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 252
Default Referencing across projects

Hi Mike,

Thank you for your help! I think you give me one method, but I still want to
know how to add a reference entry in the VBE project property window.For
example, if I open book1 and book2, how can I add a reference to book1 in
book2' VBA Project propery window.

Clara
--
thank you so much for your help


"Mike H" wrote:

Clara,

This runs the macro 'Test' in Book1.xls in the root of C

Sub runit()
Set wbTarget = Workbooks.Open("c:\book1.xls")
Application.Run (wbTarget.Name & "!test")
End Sub

You would probably want to include eror checking.

Mike

"clara" wrote:

Hi all,

How to reference a macro defined in another workbook?

Clara
--
thank you so much for your help

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Referencing across projects

Clara,

Open the workbook to which you want to set a reference. In VBA, go to the
Tools menu and choose "VBAProject Properties". In that dialog, change the
project name from "VBAProject" to something more meaningful, such as
"MyProcedures". Close that dialog and then save, but don't close, the
workbook. Then open the workbook that will reference MyProcedures. Go to the
Tools menu, choose References, and select "MyProcedures" in the list of
references. Note that VBA References use the VBProject name, not the file
name. "MyProcedures" is the project name, not the file name. The actual file
name is irrelevant.

Once the reference is in place, you can call procedures in the MyProcedures
project as if they existed the same workbook from which they are called. If
there is a possibility of a name collision (two procedures with the same
name residing in separate projects), you can qualify the procedure name with
the project name to specify which procedure to call. E.g.,

Result = MyProcedures.MyFunction(123)
' rather than
Result = MyFunction(123)

Note that you cannot close the MyProcedures workbook if there are other
workbooks open that reference that workbook.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"clara" wrote in message
...
Hi Mike,

Thank you for your help! I think you give me one method, but I still want
to
know how to add a reference entry in the VBE project property window.For
example, if I open book1 and book2, how can I add a reference to book1 in
book2' VBA Project propery window.

Clara
--
thank you so much for your help


"Mike H" wrote:

Clara,

This runs the macro 'Test' in Book1.xls in the root of C

Sub runit()
Set wbTarget = Workbooks.Open("c:\book1.xls")
Application.Run (wbTarget.Name & "!test")
End Sub

You would probably want to include eror checking.

Mike

"clara" wrote:

Hi all,

How to reference a macro defined in another workbook?

Clara
--
thank you so much for your help


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Referencing across projects

Is there a way to do this thru a procedure?
--
Cheers,
Ryan


"Chip Pearson" wrote:

Clara,

Open the workbook to which you want to set a reference. In VBA, go to the
Tools menu and choose "VBAProject Properties". In that dialog, change the
project name from "VBAProject" to something more meaningful, such as
"MyProcedures". Close that dialog and then save, but don't close, the
workbook. Then open the workbook that will reference MyProcedures. Go to the
Tools menu, choose References, and select "MyProcedures" in the list of
references. Note that VBA References use the VBProject name, not the file
name. "MyProcedures" is the project name, not the file name. The actual file
name is irrelevant.

Once the reference is in place, you can call procedures in the MyProcedures
project as if they existed the same workbook from which they are called. If
there is a possibility of a name collision (two procedures with the same
name residing in separate projects), you can qualify the procedure name with
the project name to specify which procedure to call. E.g.,

Result = MyProcedures.MyFunction(123)
' rather than
Result = MyFunction(123)

Note that you cannot close the MyProcedures workbook if there are other
workbooks open that reference that workbook.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"clara" wrote in message
...
Hi Mike,

Thank you for your help! I think you give me one method, but I still want
to
know how to add a reference entry in the VBE project property window.For
example, if I open book1 and book2, how can I add a reference to book1 in
book2' VBA Project propery window.

Clara
--
thank you so much for your help


"Mike H" wrote:

Clara,

This runs the macro 'Test' in Book1.xls in the root of C

Sub runit()
Set wbTarget = Workbooks.Open("c:\book1.xls")
Application.Run (wbTarget.Name & "!test")
End Sub

You would probably want to include eror checking.

Mike

"clara" wrote:

Hi all,

How to reference a macro defined in another workbook?

Clara
--
thank you so much for your help




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Referencing across projects

You can add a reference from one workbook to another via code. Suppose
you have a workbook named "HasFunction.xls" with a project name of
"projHasFunction" and contains a function named "AddThem". Now,
suppose you have a second workbook named "WantsFunction.xls". With
WantsFunction open (at this point it doesn't matter if HasFunction.xls
is open). In regular code module, use the following to set a reference
from WantsFuncxtion to HasFunxction:

Sub AddRef()
ThisWorkbook.VBProject.References.AddFromFile _
"C:\HasFunction.xls"
End Sub

Obviously, change the workbook name to the correct name. In the Macro
Security options of WantsFuncxtion.xls, you'll need to ensure that
"Trust access to the Visual Basic Project" is checked. Then, run the
code above. It will open the workbook "HasFunction" if it is not
already open.

Once the reference is established, you can call the AddThem functions
(which resides in HasFunction.xls) from code in WantsFunction.xls as
if AddThem resided in WantsFunction:

Dim D As Double
D = AddThem(11,22,33)

If there is any possibility of a name collision (two different
entities havint the same name), you can prefix the call to AddThem
with the library project name of HasFunxction.xls.

Dim D As Double
D = projHasfunction.AddThem(11, 22, 33)

In the future, opening the WantsFunction workbook wil cause the
HasFunxction workbook to open.

Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]



On Wed, 2 Dec 2009 11:30:01 -0800, Ryan H
wrote:

Is there a way to do this thru a procedure?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with projects Mike Excel Discussion (Misc queries) 0 February 10th 07 10:51 AM
Phantom VBA Projects (again) Peter T Excel Programming 6 January 15th 07 12:32 PM
Phantom VBA Projects (again) Dave Peterson Excel Programming 2 January 12th 07 05:20 PM
All open projects Todd Huttenstine Excel Programming 4 May 1st 04 04:33 PM
How to become a better programmer, post college. More projects or less projects. Matt Somers Excel Programming 1 February 12th 04 01:54 PM


All times are GMT +1. The time now is 10:21 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"