如何保存Excel 2010工作簿中的每个工作表以使用macros分隔CSV文件?
此问题与之前发布的问题非常相似: 将工作簿中的每张工作表保存为单独的CSV文件
但是,我的要求略有不同,因为我需要能够忽略具体命名的工作表(请参见下面的#2)。
我已经成功地利用这个答案发布的解决scheme: https : //stackoverflow.com/a/845345/1289884这是回应上述问题发布满足几乎所有我的要求,除了#2下面和# 3以下:
我有一个由多个工作表组成的Excel 2010工作簿,我正在寻找一个macros:
- 将每个工作表保存为一个单独的逗号分隔的CSV文件。
- 忽略特定的命名工作表(即名为TOC和表名称查找)
- 将文件保存到指定的文件夹(例如:c:\ csv)
另外理想的解决scheme:
- 创build一个包含指定文件夹内所有CSV工作表的zip文件
任何帮助将不胜感激。
缺口,
鉴于你扩大你的问题与差异,并拉链部分是一个重要的插件我已经概述了一个解决scheme下面:
- 创buildCSV文件,使用此行跳过特定的表格
Case "TOC", "Lookup"
- 将它们添加到一个Zip文件。 本节重点介绍Ron de Bruin的代码
如果代码不存在,代码将在StrMain
和StrZipped
下创buildpath
由于ActiveWorkbook
被细分为CSV文件,因此代码将在继续之前testingActiveWorkbook
是否已保存
(2)我遇到了一个我以前见过的问题,在我的Produce Excel文件夹中列出了所有MP3文件的属性,这些文件位于“My Music”文件夹中,当stringvariables传递给Shell.Application
时, 。 所以我咬紧牙关,为Zip_All_Files_in_Folder
的早期path添加了硬编码。 我评论了我的前一个variables传递,以显示我在哪里试过这个
VBA to save CSVS
Public Sub SaveWorksheetsAsCsv() Dim ws As Worksheet Dim strMain As String Dim strZipped As String Dim strZipFile As String Dim lngCalc As Long strMain = "C:\csv\" strZipped = "C:\zipcsv\" strZipFile = "MyZip.zip" If Not ActiveWorkbook.Saved Then MsgBox "Pls save " & vbNewLine & ActiveWorkbook.Name & vbNewLine & "before running this code" Exit Sub End If With Application .DisplayAlerts = False .ScreenUpdating = False lngCalc = .Calculation .Calculation = xlCalculationManual End With 'make output diretcories if they don't exist If Dir(strMain, vbDirectory) = vbNullString Then MkDir strMain If Dir(strZipped, vbDirectory) = vbNullString Then MkDir strZipped For Each ws In ActiveWorkbook.Worksheets Select Case ws.Name Case "TOC", "Lookup" 'do nothing for these sheets Case Else ws.SaveAs strMain & ws.Name, xlCSV End Select Next 'section to run the zipping Call NewZip(strZipped & strZipFile) Application.Wait (Now + TimeValue("0:00:01")) Call Zip_All_Files_in_Folder '(strZipped & strZipFile, strMain) 'end of zipping section With Application .DisplayAlerts = True .ScreenUpdating = True .Calculation = lngCalc End With End Sub
Create the ZIP file if it doesn't exist
Sub NewZip(sPath As String) 'Create empty Zip File 'Changed by keepITcool Dec-12-2005 If Len(Dir(sPath)) > 0 Then Kill sPath Open sPath For Output As #1 Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0) Close #1 End Sub
' Add the files to the Zip file
Sub Zip_All_Files_in_Folder() '(sPath As String, ByVal strMain) Dim oApp As Object Set oApp = CreateObject("Shell.Application") 'Shell doesn't handle the variable strings in my testing. So hardcode the same paths :( sPath = "C:\zipcsv\MyZip.zip" strMain = "c:\csv\" 'Copy the files to the compressed folder oApp.Namespace(sPath).CopyHere oApp.Namespace(strMain).items MsgBox "You find the zipfile here: " & sPath End Sub