开发者

Excel - VBA Question. Need to access data from all excel files in a directory without opening the files

So I have a "master" excel file that I need to populate with data from excel files in a directory. I just need to access each file and copy one line from the second sheet in each workbook and paste that into my master file without opening the excel files.

I'm not an expert at this but I can handle some intermediate macros. 开发者_StackOverflow中文版The most important thing I need is to be able to access each file one by one without opening them. I really need this so any help is appreciated! Thanks!

Edit...

So I've been trying to use the dir function to run through the directory with a loop, but I don't know how to move on from the first file. I saw this on a site, but for me the loop won't stop and it only accesses the first file in the directory.

Folder = "\\Drcs8570168\shasad\Test"
    wbname = Dir(Folder & "\" & "*.xls")

    Do While wbname <> ""

i = i + 1
ReDim Preserve wblist(1 To i)
wblist(i) = wbname
wbname = Dir(FolderName & "\" & "*.xls")

How does wbname move down the list of files?


You dont have to open the files (ADO may be an option, as is creating links with code, or using ExecuteExcel4Macro) but typically opening files with code is the most flexible and easiest approach.

  1. Copy a range from a closed workbook (ADO)
  2. ExecuteExcel4Macro
  3. Links method

But why don't you want to open the files - is this really a hard constraint?

My code in Macro to loop through all sheets that are placed between two named sheets and copy their data to a consolidated file pulls all data from all sheets in each workbook in a folder together (by opening the files in the background).

It could easily be tailored to just row X of sheet 2 if you are happy with this process


I just want to point out: You don't strictly need VBA to get values from a closed workbook. You can use a formula such as:

='C:\MyPath\[MyBook.xls]Sheet1'!$A$3

You can implement this approach in VBA as well:

Dim rngDestinationCell As Range
Dim rngSourceCell As Range
Dim xlsPath As String
Dim xlsFilename As String
Dim sourceSheetName As String

Set rngDestinationCell = Cells(3,1) ' or Range("A3")
Set rngSourceCell = Cells(3,1)
xlsPath = "C:\MyPath"
xlsFilename = "MyBook.xls"
sourceSheetName = "Sheet1"

rngDestinationCell.Formula = "=" _
    & "'" & xlsPath & "\[" & xlsFilename & "]" & sourceSheetName & "'!" _
    & rngSourceCell.Address

The other answers present fine solutions as well, perhaps more elegant than this.


brettdj and paulsm4 answers are giving much information but I still wanted to add my 2 cents.

As iDevlop answered in this thread ( Copy data from another Workbook through VBA ), you can also use GetInfoFromClosedFile().


Some bits from my class-wrapper for Excel:

Dim wb As Excel.Workbook
Dim xlApp As Excel.Application

Set xlApp = New Excel.Application
xlApp.DisplayAlerts = False  ''# prevents dialog boxes
xlApp.ScreenUpdating = False ''# prevents showing up
xlApp.EnableEvents = False   ''# prevents all internal events even being fired

''# start your "reading from the files"-loop here
    Set wb = xlApp.Workbooks.Add(sFilename) '' better than open, because it can read from files that are in use

    ''# read the cells you need...
    ''# [....]

    wb.Close SaveChanges:=False     ''# clean up workbook
''# end your "reading from the files"-loop here

''# after your're done with all files, properly clean up:
xlApp.Quit
Set xlApp = Nothing

Good luck!


At the start of your macro add

Application.ScreenUpdating = false

then at the end

Application.ScreenUpdating = True

and you won't see any files open as the macro performs its function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜