• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Referencing closed workbook

Canaries

New Member
I want to refer to a workbook (master.xlsx) in the same directory as the file containing the Excel VBA macro, without necessarily having to open it. [My basic understanding is that opening files is unnecessary so I would like to get into the good habit of not doing so, wherever possible.]


My first attempt was as below but it produced a type mismatch error on the 2nd ampersand symbol:

'

Dim master As Workbook

Set master = ThisWorkbook.Path & "" & "master.xlsx"

'

My second attempt (below) works fine but it opens the file.

'

Dim master As Workbook

Set master = Workbooks.Open(ThisWorkbook.Path & "" & "master.xlsx")

'

Can anyone advise on the best syntax to use please?
 
Dave - your suggestion produces a compile error and (if it worked) would still open the file (which I am trying to avoid doing).
 
You will need to use ExecuteExcel4Macro.


are you referring to a specific cell value? if so you could try:

[pre]
Code:
Dim Request, File_Path, File_Name, Sheet_Name, Cell_Ref, Cell_Value As String

File_Path = ThisWorkbook.path
File_Name = "P45 Requests"
Sheet_Name = "To Action" 'change to sheet required
Cell_Ref = "D1" 'change to cell required

Request = "'" & File_Path & "[" & File_Name & "]" & Sheet_Name & "'!" & _
Range(Cell_Ref).Address(True, True, -4150)

Cell_Value = ExecuteExcel4Macro(Request)
[/pre]
 
Hi, Canaries!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about your question...


If you haven't performed yet the search herein, try going to the topmost right zone of this page (Custom Search), type the keywords used in Tags field when creating the topic or other proper words and press Search button. You'd retrieve many links from this website, like the following one(s) -if any posted below-, maybe you find useful information and even the solution. If not please advise so as people who read it could get back to you as soon as possible.


If you don't have to use VBA necessarily you could use formulas like this:

='<drive>:<full path>[<file name with extension]<worksheet name>'!<cell reference>


For VBA use, check this, maybe it helps:

http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/add43181-4bf6-49c2-af94-0ccea5644475


Regards!
 
Back
Top