vba 循环浏览打开的工作簿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14412208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:15:18  来源:igfitidea点击:

Looping through opened workbooks

excelvba

提问by user1960257

i have managed to get a code which opens all files existing in a folder. now i want to run a macro(Called as donemovementReport) on these files one by one like as it runs on one i save the file then run on the next one.

我设法获得了一个代码,可以打开文件夹中存在的所有文件。现在我想在这些文件上一个一个地运行一个宏(称为 donemovementReport),就像它在一个文件上运行一样,我保存文件然后在下一个文件上运行。

The Macro donemovementreport pastes all data from these open sheets to a template. i want to save this tamplate not the opened workbook which carries the actual data.

宏 donemovementreport 将这些打开的工作表中的所有数据粘贴到模板中。我想保存这个模板而不是带有实际数据的打开的工作簿。

anybody with some idea?

有人有什么想法吗?

Sub OpenAllWorkbooks()
Set destWB = ActiveWorkbook
Dim DestCell As Range
FileNames = Application.GetOpenFilename( _
        filefilter:="Excel Files (*.csv*),*.csv*", _
        Title:="Select the workbooks to load.", MultiSelect:=True)
If IsArray(FileNames) = False Then
    If FileNames = False Then
        Exit Sub
    End If
End If
For n = LBound(FileNames) To UBound(FileNames)
    Set wb = Workbooks.Open(fileName:=FileNames(n), ReadOnly:=True)

 Next n

 'Dim i As Integer
 'i = ActiveWorkbook.AcceptAllChanges


 'For i = 1 To ActiveWorkbook




 Call donemovementReport

'Next i

 End Sub

回答by Peter L.

If I understand the input correctly, you need to loop through ALL opened workbooks. This may be achieved using Workbookscollection. Use this piece of code for that:

如果我正确理解输入,您需要遍历所有打开的工作簿。这可以使用Workbooks集合来实现。使用这段代码:

Dim wb As Workbook

For Each wb In Workbooks

    wb.AcceptAllChanges
    Call donemovementReport

Next wb

Modify the code between For...Nextas you wish or provide more input.

根据需要修改代码For...Next或提供更多输入。

Read more about referencing to workbooks in VBA: http://www.techrepublic.com/blog/10things/10-ways-to-reference-excel-workbooks-and-sheets-using-vba/967(the above code is item 3of 10 listed there).

阅读有关在 VBA 中引用工作簿的更多信息:http: //www.techrepublic.com/blog/10things/10-ways-to-reference-excel-workbooks-and-sheets-using-vba/967(上面的代码是项目那里列出了 10 个中的3个)。