【发布时间】:2020-09-10 10:47:55
【问题描述】:
这是我的 vba 脚本:
Sub ListAllFiles()
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = -1 Then
selectDirectory = .SelectedItems(1)
dFileList = Dir(selectDirectory & Application.PathSeparator & "*")
Do Until dFileList = ""
On Error Resume Next
Cells(nextRow, 1) = dFileList
nextRow = nextRow + 1
dFileList = Dir
Loop
End If
End With
End Sub
我目前不明白为什么该文件夹包含 20 个文件。 该脚本开始从第二个文件中获取文件名,直到到达最后一个文件,并且总是跳过第一个文件。
我尝试从以下位置切换:
nextRow = nextRow + 1
dFileList = Dir
到:
dFileList = Dir
nextRow = nextRow + 1
但这并没有带来任何改变。 也尝试从 SelectedItem(1) 更改为 SelectedItem(0) 但我收到错误。 接下来我尝试将 Cells(nextRow, 1) = dFileList 更改为 Cells(nextRow, 0) = dFileList
提前致谢!
编辑:我是在直到
nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 0
但是如果我想再次获取所有文件而不删除列 A 中的现有文件名
我会覆盖最后一个单元格。
解决方法如下
【问题讨论】: