2013-05-14 8 views
0

Basé sur d'autres choses que j'ai trouvées ici, j'ai fait le script suivant pour faire presque exactement ce que je veux. Il exportera toutes les feuilles sauf 4 dans un fichier Excel dans des fichiers CSV, leur ajoutera des dates et les enregistrera dans des dossiers datés. Le seul problème est qu'il renomme les feuilles exportées dans le fichier de traitement d'origine. Comment puis-je rectifier cela?Exporter toutes, sauf certaines feuilles Excel vers CSV via VBA?

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 

Répondre

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