通过宏保存为 CSV - 安静模式 - VBA 2010

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19863552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:20:54  来源:igfitidea点击:

Save as CSV by macro - quiet mode - VBA 2010

excelvba

提问by Amr El Shabini

I writing a macro to save the data in Excel sheet as CSV format - VBA 2010

我编写了一个宏将 Excel 工作表中的数据保存为 CSV 格式 - VBA 2010

I just have 2 issues at the end of the macro after everything is done.

一切都完成后,我在宏的末尾只有两个问题。

1) In the command ActiveWorkbook.SaveAsI used the option ConflictResolution:=xlLocalSessionChangesto guarantee overwriting the file if exist in the same folder with the same name, this option should overwrite the existing file in quiet mode without asking the user if he wants to overwrite the existing file or not.

1)在命令中ActiveWorkbook.SaveAs我使用了ConflictResolution:=xlLocalSessionChanges保证覆盖文件的选项,如果存在于同一个文件夹中的同名文件,这个选项应该在安静模式下覆盖现有文件,而不询问用户是否要覆盖现有文件。

It was working in the below syntax

它在以下语法中工作

ActiveWorkbook.SaveAs Filename:="C:\File1.xlsx", FileFormat:= _
  xlOpenXMLWorkbook, CreateBackup:=False, ConflictResolution:=xlLocalSessionChanges

And if a file with the same name was exist it was overwriting But this option is not working although it was working for example when saving the file in normal Excel format but it is not working when saving as CSV.

如果存在具有相同名称的文件,它会被覆盖但是此选项不起作用,尽管它在以普通 Excel 格式保存文件时起作用,但在另存为 CSV 时不起作用。

But it is not working in the below syntax

但它不适用于以下语法

ActiveWorkbook.SaveAs Filename:="C:\File1.csv", FileFormat:= _
  xlCSV, CreateBackup:=False, ConflictResolution:=xlLocalSessionChanges

What I mean by 'It is not working' is that it is not overwriting the existing file and still displaying the message that there is a file with the same name exists and is asking the user if he wants to overwrite it or not.

我所说的“它不起作用”的意思是它没有覆盖现有文件,并且仍然显示存在同名文件的消息,并询问用户是否要覆盖它。

As this is a macro so I don't want any interference from the user.

由于这是一个宏,所以我不希望用户有任何干扰。

2) When I use the command ActiveWorkbook.Closeto close the file after converting it to CSV, I have the message 'Do you want to save the changes (Yes/No).

2) 当我ActiveWorkbook.Close在将文件转换为 CSV 后使用命令关闭文件时,我收到消息“是否要保存更改(是/否)”。

I also want to save the file in quiet mode without having this message.

我还想在没有此消息的情况下以安静模式保存文件。

采纳答案by Blackhawk

For the first part, add Application.DisplayAlerts = Falseright before you do the SaveAs and add Application.DisplayAlerts = Trueright afterward. That suppresses the overwrite message and automatically saves over the old file.

对于第一部分,Application.DisplayAlerts = False在执行 SaveAs 之前添加,Application.DisplayAlerts = True然后立即添加。这会抑制覆盖消息并自动保存旧文件。

回答by Jair Batista

For the item 2, Try this:

对于项目 2,试试这个:

ActiveWorkbook.Close False

It will close without the question.

它将毫无疑问地关闭。

Jair Batista

杰尔·巴蒂斯塔