使用循环打开文件路径中的所有excel文件后,有没有办法通过vba创建工作簿变量来引用这些文件?
Posted
技术标签:
【中文标题】使用循环打开文件路径中的所有excel文件后,有没有办法通过vba创建工作簿变量来引用这些文件?【英文标题】:After opening all excel files in a file path with a loop, is there any way to reference these files by creating workbook variables through vba? 【发布时间】:2017-08-10 15:17:16 【问题描述】:Dim MyFolder As String
Dim MyFile As String
MyFolder = "C:--"
(leaving out the file path)
MyFile = Dir(MyFolder & "\*.xlsx")
Do While MyFile <> ""
Workbooks.Open fileName:=MyFolder & "\" & MyFile
MyFile = Dir
Loop
(see paragraph below)
Workbook.Open
Dim wbk1 as workbook
Set wbk1 = ActiveWorkbook
(can reference workbook like this)
wbk1.Activate
我检查了其他几个论坛,发现您可以通过首先打开它们来引用其他工作簿,然后创建一个变量并将其设置为上面第二个代码段中列出的打开的工作簿。
当我试图想办法为这个特定文件路径中的所有文件创建引用变量时,我意识到我无法在运行时动态命名不同的变量以将每个工作簿设置为不同的工作簿变量。
那么,除了完成我要完成的任务之外,还有其他方法吗?或者有没有办法动态创建变量?
【问题讨论】:
【参考方案1】:这是你的做法:(这会以新名称的模板形式打开)
Dim wb1 as Workbook
Set wb1 = Workbooks.Add(MyFile)
或者:(如果工作簿已经打开,这将失败)(如果您以后需要能够保存它,请使用这个)
Dim wb1 as Workbook
Set wb1 = Workbooks.Open(MyFile)
然后你可以像这样创建一个工作表对象:
Dim ws1 as Worksheet
Set ws1 = wb1.Worksheets(1)
然后,只要您想引用该表上的某些内容,例如 Range
或 Cell
,请务必使用 Worksheet
引用来限定它,如下所示:
Dim rng as Range
Set rng = ws1.Range("A1:B1")
【讨论】:
这是否允许我将工作簿作为工作表引用?至少按索引号 更新了我的答案,为您包括工作表参考。【参考方案2】:您可以使用数组、集合或字典来保存对多个工作簿的引用。 最好打开一个工作簿,执行您需要的操作,关闭它,然后使用相同的变量打开下一个工作簿。
注意:要在变量中正确存储工作簿,请使用@brax 提供的代码。
但是...这是你要求的:
此代码将打开文件夹中的每个工作簿,然后返回有关每个工作簿的信息。
Option Explicit 'You wouldn't believe how important this is at the top of your module!
Public Sub Test()
Dim MyFolder As String
Dim MyFiles As Collection
Dim wrkBk As Workbook
Dim sFile As String
Dim secAutomation As MsoAutomationSecurity
MyFolder = "C:\"
Set MyFiles = New Collection
'We don't want any WorkBook_Open macros to fire when we open the file,
'so remember the current setting and then disable it.
secAutomation = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
sFile = Dir$(MyFolder & "*.xls*")
Do While Len(sFile) > 0
'We don't want to open the file if it's got the same name as this one.
If sFile <> ThisWorkbook.Name Then
'Open the workbook and add it to the collection, give it a key of the file name.
Set wrkBk = Workbooks.Open(MyFolder & sFile)
MyFiles.Add wrkBk, wrkBk.Name
Set wrkBk = Nothing
End If
sFile = Dir$
Loop
'Reset macro security settings.
Application.AutomationSecurity = secAutomation
'----------------
'All files are open and ready to be referenced.
'----------------
Dim SingleFile As Variant
'List all details from each file in the immediate Window.
For Each SingleFile In MyFiles
With SingleFile
Debug.Print "Name: " & .Name & " | Sheet Count: " & .Sheets.Count & _
" | Last Row on '" & .Worksheets(1).Name & "' Column A: " & .Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row
End With
Next SingleFile
'Get the value from a specific file using the key value (Book7.xlsm)
Debug.Print MyFiles("Book7.xlsm").Worksheets("Form").Range("A6")
'Now close all the files.
For Each SingleFile In MyFiles
Debug.Print "Closing " & SingleFile.Name
SingleFile.Close SaveChanges:=False
Next SingleFile
End Sub
【讨论】:
感谢您的解决方案。我觉得你可能是对的,一次打开一个变量并使用一个变量然后关闭它会更有效。这就是我将如何进行!以上是关于使用循环打开文件路径中的所有excel文件后,有没有办法通过vba创建工作簿变量来引用这些文件?的主要内容,如果未能解决你的问题,请参考以下文章
Excel用vba按先后顺序打开一个文件夹中的N个excel工作簿,运行一段宏程序后