本文介绍了将文件夹中的XLS / XLSX文件转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VBA中编写了以下代码。当调试时,我不能找到任何问题。它不会创建也不会将任何文件转换为.CSV。

I have written the following code in VBA. When debugging, I am not able to find any problems. It is not creating nor converting any file into .CSV.

Sub SaveToCSVs()
    Dim fDir As String
    Dim Wb As Workbook
    Dim wS As Worksheet
    Dim csvWs As String, csvWb As String
    Dim extFlag As Long '0 = .xls & 1 = .xlsx extension types
    Dim fPath As String
    Dim sPath As String, dd() As String
    fPath = "C:\Users\DA00358662\Documents\XLSCONV\*.*"

    sPath = "C:\Users\DA00358662\Documents\XLSCONV\"
    fDir = Dir(fPath)
    extFlag = 2
    Do While (fDir <> "")
        If Right(fDir, 4) = ".xls" Or Right(fDir, 5) = ".xlsx" Then
            extFlag = 0
        Else
            extFlag = 2
        End If
        On Error Resume Next
        If extFlag = 0 Then
            fDir = Dir
            Set Wb = Workbooks.Open(fPath & fDir)
            csvWb = Wb.Name
            dd = Split(csvWb, ".")
            For Each wS In Wb.Sheets
                wS.SaveAs dd(0) & wS.Name & ".csv", xlCSV
            Next wS
            Wb.Close False
            Set Wb = Nothing
            fDir = Dir
            On Error GoTo 0
        End If
    Loop
End Sub


推荐答案

您可以连接 fPath fDir 以打开您的工作簿,您会得到类似的结果:

The Moment you concatenate fPath and fDir to open your Workbook, you get something like:

"C:\Users\DA00358662\Documents\XLSCONV\*.*MyWorkbook.xls"

请注意 *。* 我想在这里使用 sPath

Note *.* in the middle ruining your day. I think you want to use sPath here?

这篇关于将文件夹中的XLS / XLSX文件转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 20:02