【问题标题】:Save .xlsx files in a folder to .csv files将文件夹中的 .xlsx 文件保存为 .csv 文件
【发布时间】:2019-12-14 12:05:18
【问题描述】:

我尝试使用此脚本将 xlsx 文件转换为 csv。

我希望旧文件位于文件夹中,并且 csv 文件上的名称与 xlsx 文件完全相同。

我在 filename..csv 之类的 csv 扩展上获得了额外的 .

Sub ConvertCSVToXlsx()

    Dim myfile As String
    Dim oldfname As String, newfname As String
    Dim workfile
    Dim folderName As String

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

'   Capture name of current file
    myfile = ActiveWorkbook.Name

'   Set folder name to work through
    folderName = "C:\Test\"

'   Loop through all CSV filres in folder
    workfile = Dir(folderName & "*.xlsx")
    Do While workfile <> ""
'       Open CSV file
        Workbooks.Open Filename:=folderName & workfile
'       Capture name of old CSV file
        oldfname = ActiveWorkbook.FullName
'       Convert to XLSX
        newfname = folderName & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & ".CSV"
        ActiveWorkbook.SaveAs Filename:=newfname, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close
'       Delete old CSV file
        Kill oldfname
        Windows(myfile).Activate
        workfile = Dir()
    Loop

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub

【问题讨论】:

  • 这个 Len(ActiveWorkbook.Name) - 4) 应该是 Len(ActiveWorkbook.Name) - 5)
  • 这里发生了很多事情,对于一个通用问题来说真的太多了。可以通过将代码剥离到显示问题的最低限度来改进这个问题。但是,感谢您第一次为 StackOverflow 做出贡献。我会给第一篇文章+1。考虑如何将代码简化为最少的代码来解决问题。
  • @M.K.Hunter-代码的哪​​一部分对你来说太多了,你对削减它有什么建议?

标签: excel vba


【解决方案1】:

非常接近。 您的 cmets 在代码中有点混乱。

如果您要使用 left(len()-4 那么您需要更改部分以添加不带句点的 csv。 newfname = oldfname & "CSV"

对 saveas 行进行一点编辑

您不会杀死原始工作簿,而是将其从文件夹中删除。

原始工作簿不再打开,因为您将其保存为新文件名。

Sub ConvertCSVToXlsx()

    Dim myfile As String
    Dim oldfname As String, newfname As String
    Dim workfile
    Dim folderName As String
    Dim wb As Workbook
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    '   Capture name of current file
    myfile = ActiveWorkbook.Name

    '   Set folder name to work through
    folderName = "C:\New folder\"

    '   Loop through all CSV filres in folder
    workfile = Dir(folderName & "*.xlsx")
    Do While workfile <> ""
        '       Open CSV file
        Workbooks.Open Filename:=folderName & workfile
        Set wb = ActiveWorkbook
        '       Capture name of old CSV file
        oldfname = Left(wb.FullName, Len(wb.FullName) - 4)
        '       Convert to XLSX
        newfname = oldfname & "CSV"
        wb.SaveAs Filename:=newfname, FileFormat:=xlCSV, CreateBackup:=False
        wb.Close
        workfile = Dir()
    Loop

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub

【讨论】:

  • 它工作正常,但对于信用卡号码,它将进行科学注释。请问有没有办法解决这个问题??
  • 感谢 halfer 和 Davesexcel 的宝贵意见
  • 最好将csv文件导入excel,不要尝试从windows资源管理器打开。转到数据选项卡并选择“获取外部数据”,然后选择“从文本”。做一个 webSearch ,“将 CSV 文件导入 excel”
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多