Solved

Opening several workbooks

Posted on 2013-12-13
6
185 Views
Last Modified: 2013-12-16
Using vba and Excel - I may have 5 workbooks where I need to find Emp ID in the oldest workbook even though it may be in several workbooks.   What is the best way to approach this?
0
Comment
Question by:leezac
  • 3
  • 2
6 Comments
 
LVL 35

Assisted Solution

by:[ fanpages ]
[ fanpages ] earned 250 total points
ID: 39717196
Hi,

Determine the "oldest" workbook (by gaining either the "Date Created" or "Date Last Modified" of each) of the five workbooks, & then open just the workbook with the earliest date (depending what your definition of "oldest" will be).

Then search that single workbook for the "Emp ID" required.

You may have to determine what the course of action will be if two (or more) files share the same "oldest" date.

BFN,

fp.
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 250 total points
ID: 39717239
Yes, for only 5 workbooks, the brute force method is probably best if it's a one-time operation.
If you need to do this over and over, you'll need a macro to check timestamps of the files, open them, search and (if needed) close and repeat for the next oldest.  When done it should either send a message telling you which file contained the item or tell you that the item was not found.
There are some other strategic issues with this, like telling the macro where to find the workbooks (especially if they're not in a fixed location) and what if they timestamp updates...do you want the one that was CREATED earliest, or MODIFIED earliest? And there may be some other things, like is the EMP id on a fixed page ("Sheet1" or would you need to search every sheet in the workbook?  Will the cell contain the exact number or will it be mixed with other things, etc.
0
 

Author Comment

by:leezac
ID: 39717245
If the oldest does not have the EMPID do I close it
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 22

Expert Comment

by:rspahitz
ID: 39717256
That's up to you, but that would probably make sense, especially if you increase from 5 to a dozen or more workbooks.
0
 

Author Comment

by:leezac
ID: 39717267
Can I get a code example

is this a do while or if then else>>
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39721719
I can't create the exact code on my current system, but see if this helps...

Dim strFileName as String
strFileName = Dir("*.xls")
Do
   ProcessExcelFile(strFileName)

   strFileName = Dir
Loop strFileName <> ""

...

Open in new window

Sub ProcessExcelFile(FileName as String)
   Dim xlFile as WorkBook
   xlFile.Open(FileName)
   '...
   xlFile.Close
End Sub

Open in new window


Again, this may not be exact, but should be somewhat close...the harder part is working with the opened file to dig in and check all the pieces, since you'll have to examine the workbook's sheet's cell's content.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
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…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

809 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