简体   繁体   English

使用VBA将文件另存为CSV-当前将多个文件另存为工作表

[英]Saving files as CSV using VBA - currently saving multiple files as worksheets

My original problem was finding out how to separate a file with 12000 rows into individual files with 1000 rows and keep the header. 我最初的问题是找出如何将12000行的文件分隔为1000行的单个文件并保留标题。 I found a great Q&A that does just that (with only one adjustment to number of rows), the link is here: How to split spreadsheet into multiple spreadsheets with set number of rows? 我发现了一个很好的问答集(仅对行数进行了一次调整),链接位于此处: 如何将电子表格拆分为具有设定的行数的多个电子表格?

The only thing I need is for the script to save the files down as CSVs rather than Worksheets, but I can't see anything obvious in the script that defines the file type for me to change. 我唯一需要的是脚本将文件另存为CSV而不是工作表,但是在脚本中看不到任何明显的东西来定义要更改的文件类型。 FYI, I'm very new to this! 仅供参考,我对此陌生!

Thanks in advance 提前致谢

As mentioned by @vba4all in the comments above, the expression for Workbook.SaveAs is 如上述评论中@ vba4all所述, Workbook.SaveAs的表达式为

expression .SaveAs(FileName, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution, AddToMru, TextCodepage, TextVisualLayout, Local)

You can visit this msdn article to read more about it. 您可以访问此msdn文章以了解有关内容的更多信息。

To save the workbook as csv, you have to use 要将工作簿另存为csv,您必须使用

ActiveWorkbook.SaveAs Filename:="C:\Test.csv", FileFormat:=xlCSV

Please note that when you save the workbook as a csv with or without code, Excel will prompt you for confirmation. 请注意,将工作簿另存为csv(带有或不带有代码)时,Excel会提示您进行确认。 To avoid that you can set Application.DisplayAlerts to False 为了避免这种情况,您可以将Application.DisplayAlerts设置为False

Here is an example 这是一个例子

Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="C:\Test.csv", FileFormat:=xlCSV
Application.DisplayAlerts = True

To read more about file formats, you may want to see this msdn link 要了解有关文件格式的更多信息,您可能需要查看此msdn链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM