【问题标题】:Converting XLS/XLSX files in a folder to CSV将文件夹中的 XLS/XLSX 文件转换为 CSV
【发布时间】:2015-10-29 15:30:40
【问题描述】:

我在 VBA 中编写了以下代码。调试时,我找不到任何问题。它不会创建或将任何文件转换为 .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

【问题讨论】:

  • !?!?!?!?嗨,您想为每个同名的工作簿保存每张工作表!??!?!因为如果我看到你的代码(没有错误)就会发生这种情况
  • wS.SaveAs dd(0) & wS.Name & ".csv",xlCSV 是我在那里使用的实际行。那只是为了查看是否会创建至少一个 .csv。请看这条线,我已经编辑过了。谢谢指正。

标签: excel csv vba


【解决方案1】:

使用此代码(我使用的标准),您可以找到您需要的(根据需要进行修改)。 简而言之,代码询问要循环哪个目录以及对于每个具有相应扩展名的文件,在此目录中打开文件,在某个目录中另存为 csv,然后关闭原始文件。

Sub SaveAsCsv()
Dim wb As Workbook
Dim sh As Worksheet
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then Exit Sub

'Target File Extension (must include wildcard "*")
  myExtension = "*.xls*"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
    Set wb = Workbooks.Open(Filename:=myPath & myFile)
    nameWb = myPath & Left(myFile, InStr(1, myFile, ".") - 1) & ".csv"
    ActiveWorkbook.SaveAs Filename:=nameWb, FileFormat:=xlCSV
    ActiveWorkbook.Close savechanges:=False
    'Get next file name
      myFile = Dir
  Loop
'Reset Macro Optimization Settings
  Application.ScreenUpdating = True
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
End Sub

【讨论】:

  • 这个答案真的帮我节省了几个小时。非常感谢!
  • 谢谢,除了小错误外完美运行,在声明语句中将变量 nameWb 声明为字符串
  • 请注意,默认情况下,您将获得日期和数字的英语语言环境。要保留与手动单击“另存为 -> CSV”相同的格式,可以在 ActiveWorkbook.SaveAs 行的末尾添加 ,Local:=True
【解决方案2】:

连接 fPathfDir 以打开工作簿的那一刻,您会得到如下信息:

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

注意 *.* 在中间毁了你的一天。我想你想在这里使用sPath

【讨论】:

  • 非常感谢!这就是我无法生成工作表的真正原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2021-01-27
相关资源
最近更新 更多