简体   繁体   English

Excel-VBA问题。 需要访问目录中所有Excel文件中的数据而无需打开文件

[英]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. 因此,我有一个“主” excel文件,需要用目录中excel文件中的数据填充。 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. 我只需要访问每个文件,然后从每个工作簿的第二张表中复制一行,然后将其粘贴到我的主文件中,而无需打开excel文件。

I'm not an expert at this but I can handle some intermediate macros. 我不是专家,但是我可以处理一些中间宏。 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. 因此,我一直试图使用dir函数通过循环遍历目录,但是我不知道如何从第一个文件继续前进。 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? wbname如何下移文件列表?

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. 不必打开文件(ADO可能是一种选择,如创建的代码链接,或使用ExecuteExcel4Macro),但通常与代码打开文件是最灵活和最简单的方法。

  1. Copy a range from a closed workbook (ADO) 从封闭的工作簿(ADO)复制范围
  2. ExecuteExcel4Macro 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). 我在Macro中的代码循环遍历放置在两个命名工作表之间的所有工作表,并将其数据复制到统一文件中,从而将文件夹中每个工作簿中所有工作表的所有数据收集到一起(通过在后台打开文件)。

It could easily be tailored to just row X of sheet 2 if you are happy with this process 如果您对此过程感到满意,则可以轻松地将其定制为仅位于工作表2的X行

I just want to point out: You don't strictly need VBA to get values from a closed workbook. 我只想指出:您严格不需要VBA从封闭的工作簿中获取值。 You can use a formula such as: 您可以使用以下公式:

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

You can implement this approach in VBA as well: 您也可以在VBA中实现此方法:

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. 其他答案也提供了很好的解决方案,也许比这更优雅。

Some bits from my class-wrapper for Excel: 我的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! 祝好运!

brettdj and paulsm4 answers are giving much information but I still wanted to add my 2 cents . brettdjpaulsm4的答案提供了很多信息,但我仍然想加2美分

As iDevlop answered in this thread ( Copy data from another Workbook through VBA ), you can also use GetInfoFromClosedFile() . iDevlop在此线程中回答( 通过VBA从另一个工作簿复制数据 )时,您也可以使用GetInfoFromClosedFile()

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. 并且在宏执行其功能时您不会看到任何文件打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过访问VBA浏览和编辑Excel文件 - Browse and edit excel files via access vba 将数据从访问导入到打开的Excel电子表格中? (Excel VBA) - Importing data from access into an open excel spreadsheet? (Excel VBA) Excel VBA 复制文件夹,移动所有文件 - 但如果文件存在则跳过 - Excel VBA copy folder, move all files - but if file exists skip 如何使用 VBA 使用来自其他 excel 文件的新数据覆盖数据库中的数据? - How to overwrite the data in database with new data coming from other excel files using VBA? 如何从Powershell中的目录解析单个Excel文件 - How to parse individual excel files from a directory in Powershell 从文件夹中的多个文本文件中提取数据到excel工作表中 - extract data from multiple text files in a folder into excel worksheet 数据库查询:从Excel电子表格导入数据。 没有它,就无法打开该电子表格。 - Database Query: Importing data from an excel spreadsheet. Without it opening that spreadsheet. 如何在黄瓜中使用数据驱动框架来访问外部文件,例如excel或数据库 - How to use data driven framework in cucumber to access external files such as excel or data base 将数据从 Excel 导出到 SQL 服务器 - VBA - Exporting Data from Excel to SQL Server - VBA 定期从Excel文件更新数据集的策略 - Strategy for regularly updating datasets from excel files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM