宏DIR()列出文件夹和目录()列表子文件夹错误

时间:2017-06-28 16:55:48

标签: excel-vba directory vba excel

我制作了一个列出路径文件夹的宏,然后使用列表1的数据,我得到了一个包含2个文件夹的列表。但它看起来当列出2路径保存在1的数据时,我不知道如何使每个路径都有它的列表。

'mySourcePath Is the main route of the 1 list
 Sub ListMyFiles(mySourcePath)
' List the files and directories within "C:\".
mySourcePath = mySourcePath & "\"
Dim fList As String 
Dim fLists As String
Dim fName As String
Dim fNames As String
Dim numero As String
numero = 0
iRow = 2
For inicios = 0 To Total
fName = Dir(mySourcePath, vbDirectory)
'The variable fName now contains the name of the first file or directory within "C:\".
If InStr(fNames, ".") > 0 Then
    numero = Left(fNames, InStr(fNames, ".") - 1)
    ElseIf InStr(fNames, "..") > 0 Then
    numero = Left(fNames, InStr(fNames, "..") - 1)
End If
If numero = "" Then
inicios = 0
End If
Do While fName <> ""
'Store the current file or directory name in the string fList.
fList = fList & vbNewLine & fName
datos = Range("A" & iRow).Value
aux = 0
MsgBox "Ruta 1.1: " & fList
MsgBox "Datos: " & datos
If datos <> fName Then
fName = Dir()
ElseIf datos = fName Then
   'The 2 path modifies to list other folders
    rutas = mySourcePath & fName & "\ASUNTOS\"
fNames = Dir(rutas, vbDirectory)
Do While fNames <> ""
'fLists = fLists & vbNewLine & fNames
' Get the next file or directory within "C:\".
If InStr(fNames, ".") > 0 Then
    numero = Left(fNames, InStr(fNames, ".") - 1)
    ElseIf InStr(fNames, "-") > 0 Then
    numero = Left(fNames, InStr(fNames, "-") - 1)
    ElseIf InStr(fNames, "_") > 0 Then
    numero = Left(fNames, InStr(fNames, "_") - 1)
End If
If numero = "" Then
fNames = Dir()
End If
If numero <> "" Then
iRows = 2
For inicio = 0 To Total
dato = Range("B" & iRows).Value
If dato = numero Then
Range("F" & iRows).Value = numero
ruta = fNames + numero
ActiveSheet.Hyperlinks.Add Anchor:=Cells(iRows, "E"), Address:=ruta, TextToDisplay:=numero
End If
If dato <> numero Then
End If
If dato = "" Then
End If
iRows = iRows + 1
Next inicio
fNames = Dir()
End If
Loop
iRow = iRow + 1
End If
Loop
fName = fName
fName = Dir()
iRow = iRow + 1 
Next inicios
End Sub

总计是E列中的数据量,如果它与文件夹的名称相同,我使用For运行它来购买数据。 我查看子文件夹中是否有列H的名称,称为相同,如果是,我创建超链接。 例如:

E栏:500,752,800,450,1052

H栏:Alfredo,Maria,Ana,Ethan,Alvaro

路线:  C:\ Users \ User \ Documents \ 500   C:\ Users \ User \ Documents \ 800   C:\ Users \ User \ Documents \ 450   C:\ Users \ user \ Documents \ 500 \ ASUNTOS \ Maria   C:\ Users \ user \ Documents \ 500 \ ASUNTOS \ Alvaro   C:\ Users \ User \ Documents \ 800 \ ASUNTOS \ Ana

在fList中我得到:   。   ..   500   500

我不得不离开:   。   ..   500   752

0 个答案:

没有答案