Excel VBA saveas csv,但文件输出格式错误 [英] Excel VBA saveas csv but the file output is the wrong format

查看:372
本文介绍了Excel VBA saveas csv,但文件输出格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是vba的新手,我想知道为什么当我尝试此代码时文件输出只是默认文件而不是csv.我通过宏录制创建了此代码.我希望能够在工作表上将文件另存为csv.我的代码如下

HI i am new to vba and am wondering why the file output when i attempted this code just a default file and not csv. I created this code via macro recording. I want to be able to save the file as csv after working on the sheet. My code is as follows

Sub Done()
        Sheets("Test").Select
        Range("A2:M3").Select
        Range("A2:M3").Copy
        Range("A2").PasteSpecial xlPasteValues
        Application.CutCopyMode = False

        Sheets("Test").Select
        ActiveWorkbook.Save

        ActiveWorkbook.SaveAs Filename:= _
            Application.GetSaveAsFilename, FileFormat:= _
            xlCSV, CreateBackup:=False
            ActiveWorkbook.Close SaveChanges:=False

    End Sub

推荐答案

要将文件另存为csv,请尝试:

To save the file as csv try:

Sub Done()
    Sheets("Test").Select
    Range("A2:M3").Select
    Range("A2:M3").Copy
    Range("A2").PasteSpecial xlPasteValues
    Application.CutCopyMode = False

    Sheets("Test").Select
    ActiveWorkbook.Save

    ActiveWorkbook.SaveAs Filename:="C:\filename.csv", _ 
        FileFormat:=xlCSV, CreateBackup:=False
        ActiveWorkbook.Close SaveChanges:=False

End Sub

根据需要更改文件路径和文件名.

Change file path and file name as required.

编辑:使用另存为"对话框
________________________________________________________________________________

EDIT: Using SaveAs Dialog Box
________________________________________________________________________________

尝试一下:

Sub Done()
    Sheets("Test").Select
    Range("A2:M3").Select
    Range("A2:M3").Copy
    Range("A2").PasteSpecial xlPasteValues
    Application.CutCopyMode = False

    Sheets("Test").Select
    ActiveWorkbook.Save

    fname = Application.GetSaveAsFilename(InitialFileName:="Test", FileFilter:="CSV (Comma delimited) (*.csv), *.csv", Title:="Save As")
    ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=xlCSV
End Sub

这篇关于Excel VBA saveas csv,但文件输出格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆