简体   繁体   English

使用宏从单元格将Excel工作表保存为CSV文件格式的CSV文件

[英]Saving excel worksheet to CSV with file name from a cell using a macro

i have a macro that saves a copy of my workbook that works but it saves as a .xlsm and i need it to be saved as a comma delimited .csv file type can any one help me? 我有一个宏,可以保存工作簿的副本,但可以将其保存为.xlsm,并且我需要将其另存为以逗号分隔的.csv文件类型,有谁可以帮助我吗?

here is the macro i have now 这是我现在拥有的宏

Sub toCSV() 子到CSV()

 Dim newWB As Variant Dim wb1 As Workbook Set wb1 = ActiveWorkbook With wb1 .SaveCopyAs ("C:\\Users\\sales\\desktop\\") & Range("A2").Text & ".xlsm" End With End Sub 

This code will take the sheet you want to be saved as a CSV, and copy it to a new workbook before saving 此代码会将您想要保存为CSV的工作表,并在保存之前将其复制到新工作簿中

Dim CSVBook As Workbook
Set CSVBook = Workbooks.Add
ThisWorkbook.Sheets("TheCSVSheet").Copy Before:=CSVBook.Sheets(1)
CSVBook.SaveAs Filename:="C:\tmp\test.csv", FileFormat:=xlCSV
CSVBook.Close

This will allow you to save the file as a CSV, but still retain the original macro enabled spreadsheet which you can go back to, and do whatever other processing you need 这将使您可以将文件另存为CSV,但仍保留原始的启用了宏的电子表格,您可以返回该电子表格,并进行所需的其他处理

Code: 码:

.SaveAs Filename:=("C:\Users\sales\desktop\") & Range("A2").Text & ".csv", _
FileFormat=:xlCSV

Try adding this to your code: 尝试将其添加到您的代码中:

FileFormat:=xlCSVMSDOS

And changing the '.xlsm' to '.csv' 并将“ .xlsm”更改为“ .csv”

I also highly suggest you look here for future questions with regard to saving! 我也强烈建议您在这里寻找有关储蓄的未来问题!

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

相关问题 Excel宏,用于更改单元格值以匹配工作表的名称 - Excel Macro for Changing cell value to match the name of the worksheet 用于保存 Excel 工作表的宏,其中包含来自单元格值的文件名和路径 - Macro to save Excel sheet with file name and path from cell values 使用宏从Excel工作表复制数据到Excel工作表 - Copying data from/to excel worksheet using a macro 用于将工作表名称添加到单元格的宏 - Macro for adding a worksheet name to a cell 从IronPython将Excel工作表另存为文本文件 - Saving an Excel worksheet as a Text file from IronPython 宏将csv文件导入excel非活动工作表 - macro to Import csv file into an excel non active worksheet VBA根据不同工作表上单元格中的名称将图表从excel选项卡中拉到特定的文件夹和名称文件 - VBA to exort chart from excel tab to specific folder and name file based on a name in a cell on a different worksheet Excel宏可从工作表复制单元格数据并根据特定条件粘贴到另一个工作表上 - Excel macro to copy cell data from a worksheet and paste on another worksheet based on a certain condition 将第一个工作表从CSV文件复制到已启用宏的工作簿 - Copy first worksheet from CSV file to macro-enabled workbook Excel 中的宏复制工作表(通过引用每个单元格) - Macro in Excel to Copy a Worksheet (by referencing every cell)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM