2012-03-24 2 views
1

이 질문은 이전에 게시 된 질문과 매우 유사합니다 : Save each sheet in a workbook to separate CSV filesExcel 2010 통합 문서의 각 시트를 저장하여 매크로가있는 CSV 파일을 분리하려면 어떻게합니까?

는하지만, 내 요구 사항은 내가 특별히라는 이름의 워크 시트 (아래 # 2 참조) 무시 할 수있는 능력을 가질 필요가 있다는 점에서 약간 다릅니다.

I : 위의 거의 모든 아래 # 2의 예외 아래 # 3 내 요구 사항을 충족 https://stackoverflow.com/a/845345/1289884 질문에 대한 응답으로 게시했습니다 : 나는이 답변에 게시 된 솔루션을 활용에 성공했다

여러 워크 시트로 구성되어 엑셀 2010 통합 문서를하고 난 것이다 매크로를 찾고 있어요 :

  1. 저장 별도의 쉼표로 각 워크 시트는 CSV 파일을 구분.
  2. 특정라는 이름의 워크 시트 (들)을 무시 (즉, 시트 이름 TOC 및 시트 이름 조회) 지정된 폴더 (예 : C : \ CSV)에
  3. 저장 파일

이상적인 솔루션 추가 것 :

  1. 는 어떤 도움을 크게 감상 할 수

지정된 폴더 내에서 CSV 워크 시트의 모든 구성 zip 파일을 만듭니다.

+0

[매크로를 사용하여 각 시트를 Excel 통합 문서에 저장하여 CSV 파일을 구분할 수 있습니다.] (http://stackoverflow.com/questions/59075/macro-to-save-each-sheet-in-an-excel-workbook -to-separate-csv-files) – bernie

+0

SO *에는 보너스 포인트 시스템이 있지만 사용하지는 않습니다. 사기꾼! – bzlm

+0

비슷한 응답으로 제시된 해결책이 본인의 요구 사항을 해결하지 못합니다. 그 스레드에 게시해야한다면 사과드립니다. –

답변

2

닉,

당신이 차이 귀하의 질문에 확장을 감안할 때, 그리고 지퍼 부분은 내가 그 아래의 솔루션 요약 한 중요한 부가 기능이다 : 특정 시트를 건너 뛰는,

  1. 는 CSV 파일을 작성을 이 라인 사용 Case "TOC", "Lookup"
  2. Zip 파일에 추가합니다. 이 섹션에서는 ActiveWorkbook가 저장되는 코드의 테스트 파일을 이미 CSV로 서브 나누어 가져옵니다 ActiveWorkbook으로

    존재하지 않는 경우 코드는 StrMainStrZipped 아래의 경로를 생성합니다 Ron de Bruin's code here

에 크게 그립니다 진행하기 전에

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 
01을 시도 어디 보여 내 이전 변수 전달에서 주석
+0

고맙습니다. 진심으로 감사드립니다. 훌륭한 일. –

+0

매크로가 실행되면 원래 위치에 파일을 원래 형식으로 저장하는 방법은 무엇입니까? –

+0

@NickFlorez는 코드 시작 부분에'ActiveWorkbook.Save'를 추가합니다. – brettdj

관련 문제