Solved

GET DATA FROM CLOSED EXCEL WORKBOOK

Posted on 2014-03-04
11
3,279 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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
 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using MS Code on my Mac 6 67
Recommendation vb6 to vb.net or others 14 141
VS2015 Redefinition errors 4 47
VIDEO DATASOURCE Control Resize issue 2 14
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 …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

815 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