1

I am making a Macro is Excel 2016 and want to reference a workbook via it's path without opening it.

This is what I have done at the moment

Dim Wb As Workbook
Dim Path As String
Dim Fd As FileDialog

Set Fd = Application.FileDialog(msoFileDialogOpen)
Fd.AllowMultiSelect = False
Fd.Show

Path = Fd.SelectedItems(1)

Set Wb = Workbooks.Open(Path)

However, the last line opens up the file.

I'd like to know the way to set Wb as the workbook where the path leads to without opening it.

6
  • You can't. WB is a Workbook object. Besides, you've already gotten a reference to the path itself, via the Path variable. Perhaps if you can describe more about what you're trying to accomplish? Commented Aug 12, 2016 at 3:10
  • Can't I store a workbook object in Wb which is the workbook the path leads to? I just need to reference that workbook so that I can retrieve some data from its cells later on. @DavidZemens Commented Aug 12, 2016 at 3:16
  • No, it just doesn't work that way because a Workbook must be open in order to be a workbook! You can retrieve data from closed Workbooks, though, if that's what you're after. Or, you can open them invisibly in a separate instance of Excel, and retrieve data that way. Commented Aug 12, 2016 at 3:18
  • Ah I see then. It's not really a big problem if the workbook opens but it'd look better if it didn't. Thank you for your help @DavidZemens. Commented Aug 12, 2016 at 3:21
  • 1
    I agree with @DavidZemens. And if you want to retrieve values from a closed workbook then see This Commented Aug 12, 2016 at 3:31

1 Answer 1

2

I just need to reference that workbook so that I can retrieve some data from its cells later on. Can't I store a workbook object in Wb which is the workbook the path leads to?

Nope. Until a file is open in Excel, it isn't a workbook, even though you & I might think of it as a workbook, it's not. It's just a file. But you can get data from closed workbooks.

There are a few ways that this is commonly done (there may be others):

  1. For small amounts of data, use ExecuteExcel4Macro method.
  2. For large amounts of data, use ADO to query the workbook.
  3. For simple solution, just open the file and keep it invisible.

I'll cover the simplest way to do that, which is to just open it invisibly in a new instance of Excel. This is only slightly more complicated than opening it visibly in the same instance of Excel. You'll just need to create a new Excel instance, set its .Visible property to False, and open the file in that instance.

Dim Wb As Workbook
Dim xlApp as Excel.Application 
Dim Path As String
Dim Fd As FileDialog

Set Fd = Application.FileDialog(msoFileDialogOpen)
Fd.AllowMultiSelect = False
Fd.Show

If Fd.SelectedItems > 0 Then
    Path = Fd.SelectedItems(1)
Else
    MsgBox "Nothing selected!"
    Exit Sub
End If

'## Create new instance of excel, and make it invisible
Set xlApp = New Excel.Application
xlApp.Visible = False
'## Open your workbook in this new instance
'   I use ReadOnly:=True to prevent conflict if the file is already open elsewhere
Set Wb = xlApp.Workbooks.Open(Path, ReadOnly:=True)

Make sure that at the end of your procedure you do this, to close the workbook and quit the new/invisible Excel instance:

Wb.Close 
xlApp.Quit
Sign up to request clarification or add additional context in comments.

1 Comment

I will try it out. Thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.