我需要基于单个单元格值索引的数千张excel(2016)表。工作簿只有1个工作表,数据始终存在于单元格D2中。
我想将D2复制到第二列中的主文件中,并在第一列中使用相关文件的名称。 各个excel文件已经分成子文件夹,有时会分为5个文件夹。
我对编码一般都很陌生,所以如果你能够逐步解释事情那么奖励积分。我想跟随学习。
感谢您提前帮助!
编辑:
我在激活工作表,更改文件,然后激活其他工作簿之前所做的事情。我迷失了下一步该做什么,因为我没有激活其他工作表,我只是从它们中提取数据。它们甚至不需要打开。
循环应调用该文件。 然后我需要:选择范围>复制>调用主文件>激活>粘贴>将1添加到行计数以将所选单元格向下移动>端
然后我只有一堆没有名字的数字,所以我想将前一个单元格命名为从中提取的文件的文件名。在这方面寻求帮助只会提取如何在文件名或路径之后命名工作表中的单元格,我想要反过来,以及从每次更改的不同来源。
代码循环:
Option Explicit
Sub deeploop()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim objFile As Object
Dim MyFolder As String
Dim wkbOpen As Workbook
Dim wkb As Workbook
Dim wks As Worksheet
Dim CalcMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Change path
MyFolder = "C:\Path"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(MyFolder)
Set wkb = ActiveWorkbook
Set wks = ActiveSheet
For Each objSubFolder In objFolder.SubFolders
For Each objFile In objSubFolder.Files
Set wkbOpen = Workbooks.Open(objFile.Path)
'code
wkbOpen.Close savechanges:=True
Next objFile
Next objSubFolder
With Application
.Calculation = CalcMode
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
答案 0 :(得分:0)
他们甚至不需要打开
所以不要打开它们!
只需在活动工作表中放置一个公式,该公式引用完全符合文件路径,文件名和工作表名称的正确单元格
假设所有数千个excel工作簿的唯一表单以“Sheet1”命名,您可以按如下方式操作:
Option Explicit
Sub deeploop()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim objFile As Object
Dim MyFolder As String
Dim CalcMode As Long
Dim ifile As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Change path
MyFolder = "C:\Path"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(MyFolder)
With ActiveSheet.Range("A1:B1") '<--| it suffices to reference the 'ActiveSheet' object since it belongs to 'ActiveWorkbook' by default
For Each objSubFolder In objFolder.SubFolders
For Each objFile In objSubFolder.Files
.Offset(ifile).Value = Array(objFile.Name, "='" & objSubFolder.Path & "\[" & objFile.Name & "]Sheet1'!$D$2")
ifile = ifile + 1
Next objFile
Next objSubFolder
End With
With Application
.Calculation = CalcMode
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub