尼克,
给你扩大了与不同的问题,拉链部分是我提出下面一个解决方案显著插件:
- 创建CSV文件,跳过特定工作表使用此行
Case "TOC", "Lookup"
- 将它们添加到Zip文件。本节大量借鉴Ron de Bruin's code here
的代码将创建StrMain
和StrZipped
下的路径,如果他们不已经存在
由于ActiveWorkbook
被细分为CSV文件的ActiveWorkbook
保存的代码测试在进行之前
On(2)我遇到了我之前在Produce an Excel list of the attributes of all MP3 files that sit in or below the "My Music" folde中遇到的问题,其中Shell.Application
在将字符串变量传递给它时发生了错误。所以我咬紧牙关,并为Zip_All_Files_in_Folder
增加了一个硬编码的早期路径。我注释掉我刚才的变量传递到显示在那里我想这
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
的可能重复的[宏每个片保存在Excel工作簿分开CSV文件](http://stackoverflow.com/questions/59075/macro-to-save-each-sheet-in-an-excel-workbook -to-separate-csv-files) – bernie 2012-03-24 11:39:12
SO *有*奖励积分系统,但你没有使用它。骗子! – bzlm 2012-03-24 11:39:51
类似响应中提供的解决方案未解决我的要求。我很抱歉,如果我应该发布到该线程。 – 2012-03-24 11:46:22