Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

GET DATA FROM CLOSED EXCEL WORKBOOK

Posted on 2014-03-04
11
Medium Priority
?
4,219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 35

Accepted Solution

by:
Norie earned 1800 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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 35

Expert Comment

by:Norie
ID: 39904299
jmcmunn

The workbook is not being opened.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 200 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 35

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 35

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
Video by: Zack
Viewers will learn the basics of using Excel Tables, the benefits found with them, and some pitfalls.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

618 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