vba 如何在VBA中保存工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43568810/
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 12:30:00  来源:igfitidea点击:

how to save a sheet in VBA

vbaexcel-vbacsvsavexlsm

提问by T.G.

I'm building a form that when you fill it up, it should generate 2 files as output.

我正在构建一个表单,当你填写它时,它应该生成 2 个文件作为输出。

In order to do that I have 2 sheets in the background that I fill up depending on what the user entered a button to save them (as csv).

为了做到这一点,我在背景中有 2 张纸,我根据用户输入的按钮来保存它们(作为 csv)填充。

This is the code for saving the sheet-

这是保存工作表的代码-

Worksheets("worksheetname").SaveAs Filename:="C:\path" & name & ".csv", FileFormat:=xlCSV, CreateBackup:=False

The problem is that after the user presses the button, the form that he is filling is now 'saved as csv', and not xlsm like before.

问题是在用户按下按钮后,他正在填写的表单现在“另存为 csv”,而不是像以前那样 xlsm。

Is there a way to avoid that from happening? to generate the output without affecting the form?

有没有办法避免这种情况发生?在不影响表单的情况下生成输出?

Thanks!

谢谢!

回答by A.S.H

To keep the original workbook intact, you need to fork a copyof the worksheet before saving it as CSV. Try this:

要保持原始工作簿完整无缺,您需要在将工作表保存为 CSV 之前派生一份工作表副本。尝试这个:

Application.DisplayAlerts = False
Worksheets("worksheetname").copy '  Forks a copy of the sheet in a new, active WB
With ActiveWorkbook
    .SaveAs Filename:="C:\Path\" & name & ".csv", FileFormat:=xlCSV, CreateBackup:=False
    .Close False
End With
Application.DisplayAlerts = True