以下代码存在文件名或文件号错误,请帮我修复 Sub FindFolder() Dim path As String Dim cellRange As Range Dim outputRange As Range Dim folderName As String Dim folderList As String Dim cell As Range '设置路径和目标单元格范围 path = Range("E7").Value Set cellRange = Range("E7:E14") Set outputRange = Range("E16:E26") '循环遍历目标单元格 For Each cell In cellRange folderName = cell.Value '检查文件夹是否存在 If Dir(path & "" & folderName, vbDirectory) <> "" Then '如果存在,则检查文件夹中是否包含X字符 If InStr(folderName, "X") > 0 Then '如果包含,则将文件夹名称添加到列表中 folderList = folderList & folderName & vbNewLine End If End If Next cell '将结果输出到目标单元格 outputRange.Value = Split(folderList, vbNewLine) End Sub
时间: 2023-06-19 11:10:09 浏览: 121
判断文件(文件夹)是否存在
3星 · 编辑精心推荐
Sub FindFolder()
Dim path As String
Dim cellRange As Range
Dim outputRange As Range
Dim folderName As String
Dim folderList As String
Dim cell As Range
' 设置路径和目标单元格范围
path = Range("E7").Value
Set cellRange = Range("E7:E14")
Set outputRange = Range("E16:E26")
' 循环遍历目标单元格
For Each cell In cellRange
folderName = cell.Value
' 检查文件夹是否存在
If Dir(path & "\" & folderName, vbDirectory) <> "" Then
' 如果存在,则检查文件夹中是否包含X字符
If InStr(folderName, "X") > 0 Then
' 如果包含,则将文件夹名称添加到列表中
folderList = folderList & folderName & vbNewLine
End If
End If
Next cell
' 将结果输出到目标单元格
outputRange.Value = Split(folderList, vbNewLine)
End Sub
注:修改了代码中路径拼接符号应为 "\",而非 ""。
阅读全文