2013-05-14 67 views
0

基於我在這裏找到的其他一些東西,我做了以下腳本來完成我想要的東西。它會將excel文件中除4個特定頁面外的所有頁面導出爲CSV文件,並將日期附加到它們,並將它們保存到日期文件夾中。唯一的問題是它重命名它在原始處理文件中導出的工作表。我該如何糾正這一點?通過VBA將除Excel以外的所有excel表導出爲CSV格式?

Sub SaveLCPWorksheetsAsCsv() 

Dim WS As Excel.Worksheet 
Dim SaveToDirectory As String 

Dim CurrentWorkbook As String 
Dim CurrentFormat As Long 

CurrentWorkbook = ThisWorkbook.FullName 
CurrentFormat = ThisWorkbook.FileFormat 
' Store current details for the workbook 

    SaveToDirectory = "C:\test\" & Format(Date - 1, "YYYYMM") & "\" 

    If Len(Dir(SaveToDirectory, vbDirectory)) = 0 Then 
    MkDir SaveToDirectory 
    End If 

    For Each WS In ThisWorkbook.Worksheets 
    If WS.Name <> "Input" And WS.Name <> "Ref" And WS.Name <> "Total" And WS.Name <> "Affected Accounts" Then 
     WS.SaveAs SaveToDirectory & WS.Name & "_" & Format(Date - 1, "YYYYMMDD"), xlCSV 
    End If 
    Next 

Application.DisplayAlerts = False 
    ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat 
Application.DisplayAlerts = True 
' Temporarily turn alerts off to prevent the user being prompted 
' about overwriting the original file. 

End Sub 

回答

0
Sub Tester() 

Dim ws As Worksheet, wb As Workbook 

    For Each ws In ThisWorkbook.Worksheets 
     ws.Copy 'creates a new workbook 
     With ActiveWorkbook 
      .SaveAs "C:\temp\" & ws.Name & "_blah.csv", xlCSV 
      .Close False 
     End With 
    Next ws 

End Sub