Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 对文件夹中的所有子文件夹执行相同的操作_Excel_Vba - Fatal编程技术网

Excel 对文件夹中的所有子文件夹执行相同的操作

Excel 对文件夹中的所有子文件夹执行相同的操作,excel,vba,Excel,Vba,下面给出的代码将“C:\files\Bangalore”中的所有xlsx文件转换为csv文件 Sub xlsxTOcsv() Dim sPathInp As String Dim sPathOut As String Dim sFile As String sPathInp = "C:\Files\Bangalore\" sPathOut = "C:\Files\Bangalore" Application.DisplayAlerts = False sFile = Di

下面给出的代码将“C:\files\Bangalore”中的所有xlsx文件转换为csv文件

Sub xlsxTOcsv()
Dim sPathInp    As String
Dim sPathOut    As String
Dim sFile       As String
sPathInp = "C:\Files\Bangalore\"
sPathOut = "C:\Files\Bangalore"
Application.DisplayAlerts = False
sFile = Dir(sPathInp & "*.xlsx")
Do While Len(sFile)
    With Workbooks.Open(fileName:=sPathInp & sFile)
        .SaveAs fileName:=sPathOut & Left(.Name, InStr(1, .Name, ".") - 1), _
                fileformat:=xlCSV, _
                CreateBackup:=False
        .Close SaveChanges:=False
    End With
    sFile = Dir()
Loop
Kill sPathInp & "\" & "*.xlsx"
End Sub
问题是,我的“C:\Files\”中有很多类似的文件夹,分别用于不同的城市

例如:

C:\Files\Chennai
C:\Files\德里
C:\Files\Kolkata
C:\Files\n

我在所有这些文件夹中执行相同的操作

有没有办法通过调用“C:\Files\”对所有这些子文件夹执行相同的操作


我在“C:\files\”中没有任何文件,只有子文件夹

您可以将它们添加到一个简单的数组中并在其中循环:

Sub xlsxTOcsv()
Dim sPathInp    As String
Dim sPathOut    As String
Dim sFile       As String
Dim vArr
Dim vFile

vArr = Array("Bangalore", "Chennai", "Delhi", "Kolkata")

sPathInp = "C:\Files\"
sPathOut = "C:\Files\"

Application.DisplayAlerts = False
For Each vFile In vArr
sFile = Dir(sPathInp & vFile & "\*.xlsx")
Do While Len(sFile)
    With Workbooks.Open(Filename:=sPathInp & vFile & "\" & sFile)
        .SaveAs Filename:=sPathOut & vFile & "\" & Left$(.Name, InStr(1, .Name, ".") - 1), _
                FileFormat:=xlCSV, _
                CreateBackup:=False
        .Close SaveChanges:=False
    End With
    sFile = Dir()
Loop
Kill sPathInp & vFile & "\" & "*.xlsx"
Next
Application.DisplayAlerts = True

End Sub

这里有一个通用的解决方案,您不需要知道子文件夹的名称。这将查找所有子文件夹并处理其中每个文件夹中的电子表格

您需要引用
Windows脚本主机对象模型
,方法是单击“工具”菜单,引用…,然后向下滚动并勾选
Windows脚本主机对象模型

Sub xlsxTOcsv()

Dim sPathInp    As String
Dim sPathOut    As String
Dim sFile       As String
Dim rootFolderPath As String
Dim rootFolder As Folder
Dim subFolder As Folder

rootFolderPath = "C:\Files"

''You need to add a reference to Windows Script Host Object Model

Dim fso As New FileSystemObject

Application.DisplayAlerts = False

Set rootFolder = fso.GetFolder(rootFolderPath)

For Each subFolder In rootFolder.SubFolders

    sPathInp = subFolder.Path & "\"
    sPathOut = sPathInp

    sFile = Dir(sPathInp & "*.xlsx")
    Do While Len(sFile)
        With Workbooks.Open(Filename:=sPathInp & sFile)
            .SaveAs Filename:=sPathOut & Left(.Name, InStr(1, .Name, ".") - 1), _
                    FileFormat:=xlCSV, _
                    CreateBackup:=False
            .Close SaveChanges:=False
        End With
        sFile = Dir()
    Loop
    Kill sPathInp & "*.xlsx"

Next subFolder


Application.DisplayAlerts = True

End Sub

@abdulshiyas因为您在问题中没有指定这应该适用于所有子文件夹,所以看起来(至少对我来说)您需要一个横截面。请在以后的提问中更加清楚+1用于此代码。