Solved

GET DATA FROM CLOSED EXCEL WORKBOOK

Posted on 2014-03-04
11
3,149 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

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 …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn the basics about Excel 2013’s new Flash Fill feature.
Viewers will learn a basic relationship technique in Power Pivot for Excel 2013.

920 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

12 Experts available now in Live!

Get 1:1 Help Now