0

我需要使用 VB 宏实现自动化,我必须从特定文件夹中获取一组 xls 文件并将它们转换为 csv,然后根据文件名将其隔离,例如 All File name has benifit 将被放入一个新文件夹中称为 benifit 并且所有资金将被放入一个名为funds的文件夹中(我们必须在某个路径中创建一个文件夹)。

我有代码通过硬编码路径将 xls 转换为 csv,但我不知道如何将它与名称分开。

任何想法或代码将不胜感激

提前致谢

<>

下面的代码获取文件所在的输入路径,并将它们一次转换为 csv 到输出路径

现在我想增强此代码以

 Sub ConvertXLStoCSVNoRules(mySourcePath, myKeywordPath)
Set MyObject = New Scripting.FileSystemObject
Set strInputFolder = MyObject.GetFolder(mySourcePath)
Set strOutputFolder = MyObject.GetFolder(myKeywordPath)
strInputFolder = strInputFolder & "\"
strOutputFolder = strOutputFolder & "\"
strXLSFile = Dir(strInputFolder & "*.xls*")
counter = 0
row = 13
Worksheets("Main").Cells(row, 1).Value = "Files processed at " & Now
row = row + 1
On Error Resume Next
Do While strXLSFile <> ""
    counter = counter + 1
    row = row + 1

    'strCSVFile = Left(strXLSFile, InStrRev(strXLSFile, ".")) & "csv"
    strCSVFile = Left(strXLSFile, 4) & " SL" & ".csv"

    'Add into the first sheet for recording purpose
    Worksheets("Main").Cells(row, 1).Value = strXLSFile

    Workbooks.Open strInputFolder & strXLSFile
    ActiveWorkbook.SaveAs strOutputFolder & strCSVFile, xlCSV
    ActiveWorkbook.Close False
    strXLSFile = Dir

Loop
    'MsgBox ("Files completed " & counter)
    row = row + 1
    Worksheets("Main").Cells(row, 1).Value = "Files completed " & counter & " at " & Now
End Sub
  • 根据新文件夹中的文件名对文件进行分类
  • 例如,一个文件夹中有 100 个文件,上面的代码会将其转换为 csv 并将其放在给定的路径中

但我想增强它的代码应该将 csv 或 xls 文件与文件名分开,就像所有文件在文件名中有好处一样应该来到一个名为 benifits 的新文件夹

所有带有资金的文件都应该进入一个名为funds的新文件夹,它可以在csv转换之前甚至之后发生,

请建议我最好的方法

4

1 回答 1

0

You could just change the output filename based on the input filename with code similar to the following:

Dim Index As Integer
Dim outputFolder As String
Dim specialFolders(1 To 2) As String

specialFolders(1) = "funds"
specialFolders(2) = "benifit"

outputFolder = strOutputFolder
For Index = LBound(specialFolders) To UBound(specialFolders)
    If InStr(1, strCSVFile, specialFolders(Index), vbTextCompare) > 0 Then
        outputFolder = outputFolder & specialFolders(Index) & "\"
        Exit For
    End If
Next

ActiveWorkbook.SaveAs outputFolder & strCSVFile, xlCSV
于 2013-10-01T10:26:43.757 回答