Solved

GET DATA FROM CLOSED EXCEL WORKBOOK

Posted on 2014-03-04
11
3,043 Views
Last Modified: 2014-03-04
Hi All,
how does one read/copy data from a closed workbook (without opening it?)?
At present I am using code as follows, but don't wish to open the workbook.

  Set SrcWbk = Workbooks.Open("L:\Backup\BE.xlsx", , , , "test")
  Set SrcSht = SrcWbk.Worksheets("Sheet1")
  SrcSht.Range("B2:H2").Copy
  DestWbk.Worksheets("Sheet2").Range("A1").PasteSpecial xlPasteAll

thanks
0
Comment
Question by:COACHMAN99
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 12

Expert Comment

by:jmcmunn
ID: 39904199
You can't get data from a file that is not "open" in some fashion.  You could probably work around it somehow by reading in raw bytes or something...but why would you want to?

If you're using the excel libraries as it appears you are, you are doing it the right way.
0
 
LVL 33

Accepted Solution

by:
Norie earned 450 total points
ID: 39904239
There are various ways to get data from a closed workbook.

Here's one.
Private Function GetValue(path, file, sheet, ref)
'   Retrieves a value from a closed workbook
Dim arg As String

    If Right(path, 1) <> "\" Then path = path & "\"

    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If

    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)

    GetValue = ExecuteExcel4Macro(arg)

End Function

Open in new window

This code only gets one value at time so probably isn't appropriate for a large amount of data but for 7 cells (B2:H2) should be OK to use in a loop.

Perhaps something like this.
Dim rngDst As Range

    Set rngDst = DestWbk.Worksheets("Sheet2").Range("A1")
 
    For Each cl in Range("B2:H2")

        rngDst = GetValue("L:\Backup\", "BE.xlsx", "Sheet1", cl.Address)
        Set rngDst = rngDst.Offset(,1)

    Next cl

Open in new window

0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 39904268
thanks.

it looks as if ExecuteExcel4Macro(arg) is the key. let me try it.
0
 
LVL 12

Expert Comment

by:jmcmunn
ID: 39904284
I'm sure the macro "opens" the file just like any other means of reading the data...the process will just be listed as some COM component or something.  And I've never really had MS Office COM behave like I want it to.

My 2 cents.
0
 
LVL 33

Expert Comment

by:Norie
ID: 39904299
jmcmunn

The workbook is not being opened.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 50 total points
ID: 39904329
First of all, you did not tell us where that code is running. In an Excel macro? From a script file? From a VB6 application? From a VB.NET application? That might change the answer a bit. I am assuming that you are not running the code from Excel. Otherwise, you will need to use one of the 3 other methods in order to make it work with an "apparent" closed file.

"apparent" because, as jmcmunn stated, there is no way to read from a file without opening it. No matter which technique you use, Excel has to fire in the background, although it can be hidden from the user.

One thing you can do is to set the Visible property of the Excel.Application object to False, so the application does not show on the screen. This makes the process transparent to the user, and also helps performance because Excel does not have to work on the display. In such a situation however, you must be sure to trap Exceptions carefully and make sure that the Application is closed when you are finished with it, because the user does not know that Excel is running in the background and does not have the usual interface to Close it in case the application crash.

One thing that could improve the performance however, if you have that problem and if it suits the situation, is to do most of the
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 39904371
Hi imnorie,
what data-type is cl? (couldn't find 'cell')
would you be able to place actual values in the cl.Address arg so I can 'see-monkey..'?
thanks
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 39904376
Hi jamesburger,
thanks for that. I would probably have to 'hide' the source file, but leave the des open (as this is where the code is run from)

any idea what property only applies to the workbook, not the application?
thanks
0
 
LVL 33

Expert Comment

by:Norie
ID: 39904383
cl shoud be declared as range.

Not sure what you mean about actual values of cl.Address, that's just the cell address of cl, ie $B$2, $C$2, $D$2,...,$H$2.

I could actually have used an array or some other method for that but using  a range and VBA's built-in Address property seemed easier.
0
 
LVL 33

Expert Comment

by:Norie
ID: 39904388
COACHMAN

How will you hide the source file when it's not being opened?
0
 
LVL 7

Author Closing Comment

by:COACHMAN99
ID: 39904407
thanks guys

P.S. imnorie. how do I pass the password in the ExecuteExcel4Macro arg?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Viewers will learn a basic data manipulation technique of unpivoting data in Power Query for Excel 2013 and the importance of using good data. Start with data in a poor structure: Create a table on your data: Unpivot columns: Rename columns: …
Viewers will learn how to share Excel data with others from desktop Excel, as well as Excel Online via OneDrive, and embed an Excel file on a website.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now