Excel 在当前目录中打开工作簿
我有一个主工作簿,其中包含文件列表Excel 在当前目录中打开工作簿,excel,vba,Excel,Vba,我有一个主工作簿,其中包含文件列表(E14:E26),其中一些是空白(空)单元格。我需要代码在列表中循环并打开列出的工作簿(所有工作簿都在当前目录中)。我已尝试此代码,但收到错误消息: Sub SkipBlankCells() Dim cell As Range, rng As Range, FName As String Set rng = Range("E14:E26") For Each cell In rng If Not IsEmpty(ce
(E14:E26)
,其中一些是空白(空)单元格。我需要代码在列表中循环并打开列出的工作簿(所有工作簿都在当前目录中)。我已尝试此代码,但收到错误消息:
Sub SkipBlankCells()
Dim cell As Range, rng As Range, FName As String
Set rng = Range("E14:E26")
For Each cell In rng
If Not IsEmpty(cell) Then
FName = cell.Value
Workbooks.Open Filename:=FName
End If
Next cell
End Sub
我假设单元格中不包含工作簿的整个路径,只包含文件名,因此应该可以:
Sub SkipBlankCells()
Dim cell As Range, rng As Range, FName As String
Set rng = Range("E14:E26")
For Each cell In rng
If Not IsEmpty(cell) Then
FName = vbNullString
FName = Dir(thisworkbook.Path & "\" & cell.Value) 'use dir to get the filename. If it doesn't exists, it will return blank
If Not FName = vbNullString then Workbooks.Open _
Filename:=Thisworkbook.Path & "\" & FName 'to open a workbook you need the whole path to it, not only the file name.
End If
Next cell
End Sub
这个功能应该可以工作。我认为你的功能问题在于路径。为了解决这个问题,我使用了函数
ActiveWorkbook.Path
,但为了让您知道,您可以使用文本路径,例如“C:\Users\bor\Documents\folder\u with_files”
来使用不同的路径
Sub open_file_list()
first_row = 14 'in your case
last_row = 26 'in your case
For i = first_row To last_row
If Not IsEmpty(Cells(i, 5)) Then 'Check for empty
file = Cells(i, 5)
Workbooks.Open (ActiveWorkbook.Path & "\" & file)
End If
Next
End Sub
单元格中有哪些值?它们是文件的完整路径吗?您收到了什么错误消息?“找不到文件”将需要不同的解决方案来解决“类型不匹配”问题。请使用
应用程序。PathSeparator
而不是“\”
,这甚至可以在Windows和Mac上使用。只是想知道。。。在检查如果不是空的(单元格),那么
,是否存在可能的情况,在这种情况下,如果不是FName=vbNullString,该检查将为假-?很抱歉,但它将如何崩溃@SiddharthRout?我有一个If来检查FName,如果它是空的,它就不会崩溃@Pᴇ谢谢!我不知道。工作簿。如果文件不存在,打开
将失败。