Batch convert Excel to text-delimited files

前端 未结 2 805
栀梦
栀梦 2021-01-23 14:59

Hi I\'m facing a problem on dealing with converting Excel spreadsheets to txt files.

What I want to do is to create a Macro which can takes all the xls files in one fold

2条回答
  •  臣服心动
    2021-01-23 15:36

    The issue with your code is that you define sPath as a path containing wildcard characters:

    sName = Dir(fPath & "*.xls*")
    

    and replace only the extension portion (.xls*), but leave the wildcard character before the extension in place:

    Replace(sName, ".xls*", ".txt")
    

    This produces the following path:

    C:\Users\A9993846\Desktop\*.txt
    

    which causes the error you observed, because the SaveAs method tries to save the spreadsheet to a file with the literal name *.txt, but * is not a valid character for file names.

    Replace this:

    .SaveAs Replace(sName, ".xls*", ".txt"), 42
    

    with this:

    Set wb = sh.Parent
    basename = Replace(wb.FullName, Mid(wb.Name, InStrRev(wb.Name, ".")), "")
    .SaveAs basename & "_" & sh.Name & ".txt", xlUnicodeText
    

提交回复
热议问题