:尝试打开文件夹中的所有excel文件时,代码会跳过Do之后的所有操作,而FileName<&燃气轮机&引用&引用;
所以我有点像VBA noob,但我正在努力学习 我需要我的宏打开文件夹中的所有excel文件(数百个)并提取信息,以便在一张工作表中汇总所有内容 经过长时间的搜索,我在msdn.microsoft.com上找到了一个似乎符合我需要的示例代码::尝试打开文件夹中的所有excel文件时,代码会跳过Do之后的所有操作,而FileName<&燃气轮机&引用&引用;,excel,vba,Excel,Vba,所以我有点像VBA noob,但我正在努力学习 我需要我的宏打开文件夹中的所有excel文件(数百个)并提取信息,以便在一张工作表中汇总所有内容 经过长时间的搜索,我在msdn.microsoft.com上找到了一个似乎符合我需要的示例代码: Sub MergeAllWorkbooks() Dim SummarySheet As Worksheet Dim FolderPath As String Dim NRow As Long Dim FileName
Sub MergeAllWorkbooks()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
' Modify this folder path to point to the files you want to use.
FolderPath = "C:\Users\...\Desktop\Test_Summary_Folder"
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")
' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
' Set the cell in column A to be the file name.
SummarySheet.Range("A" & NRow).Value = FileName
' Set the source range to be A9 through C9.
' Modify this range for your workbooks.
' It can span multiple rows.
Set SourceRange = WorkBk.Worksheets(1).Range("A9:C9")
' Set the destination range to start at column B and
' be the same size as the source range.
Set DestRange = SummarySheet.Range("B" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
' Increase NRow so that we know where to copy data next.
NRow = NRow + DestRange.Rows.Count
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
Loop
' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
End Sub
子合并所有工作簿()
将汇总表作为工作表进行调整
将FolderPath设置为字符串
暗淡无光
将文件名设置为字符串
将WorkBk设置为工作簿
将源范围变暗为范围
变暗减小范围为范围
'创建新工作簿并将变量设置为第一个工作表。
设置汇总表=工作簿。添加(XLWBATWORKEM)。工作表(1)
'修改此文件夹路径以指向要使用的文件。
FolderPath=“C:\Users\…\Desktop\Test\u Summary\u文件夹”
'NRow跟踪在目标工作簿中插入新行的位置。
NRow=1
'第一次调用Dir,将其指向文件夹路径中的所有Excel文件。
FileName=Dir(FolderPath&“*.xl*”)
'循环,直到Dir返回空字符串。
文件名“”时执行此操作
'在文件夹中打开工作簿
设置WorkBk=Workbooks.Open(文件夹路径和文件名)
'将列A中的单元格设置为文件名。
SummarySheet.Range(“A”&NRow).Value=FileName
'将源范围设置为A9到C9。
'修改工作簿的此范围。
'它可以跨多行。
设置SourceRange=WorkBk.Worksheets(1.Range)(“A9:C9”)
'将目标范围设置为从B列开始,然后
'与源范围的大小相同。
Set DestRange=SummarySheet.Range(“B”和NRow)
设置DestRange=DestRange.Resize(SourceRange.Rows.Count_
SourceRange.Columns.Count)
'将值从源复制到目标。
DestRange.Value=SourceRange.Value
'增加NRow,以便我们知道下一步复制数据的位置。
NRow=NRow+DestRange.Rows.Count
'关闭源工作簿而不保存更改。
WorkBk.Close savechanges:=False
'使用Dir获取下一个文件名。
FileName=Dir()
环
'在目标工作表上调用AutoFit,以便所有
"数据是可读的。
SummarySheet.Columns.AutoFit
端接头
我想更改一些部分,例如复制到工作簿中的工作表,而不是创建新的工作表,并且需要在复制数据之前向工作簿中添加一个包含公式的新工作表,但这些是额外的问题。第一:
宏只打开一个新工作簿,然后跳过之后的所有内容
Do While FileName <> "".
Do While FileName”“。
有人知道为什么吗
为了澄清,事实上,测试文件夹中有excel文件
提前感谢:-)您缺少反斜杠: 试一试
我假设您遵守了
“修改此文件夹路径以指向您要使用的文件。
并将FolderPath=“C:\Users\…\Desktop\Test\u Summary\u folder”
更改为指向真实文件夹?是的,这是我的真实文件夹,我只是取出了用户。这导致了一个运行时错误,因为它没有找到一个特定的文件名,我不知道它是如何产生的,但它给了我一个想法,在文件夹路径后添加反斜杠,反而解决了问题。@Rodario您所描述的实际上应该会产生与FunThomas的解决方案完全相同的路径字符串。@Inarion但不知怎的,它没有。
FileName = Dir(FolderPath & "\*.xl*")