VBA. Export only visible sheet to individual workbook










0














Sub SaveShtsAsBook() 
‘Select all visible and hide sheet’
Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
MyFilePath$ = ActiveWorkbook.Path & "" & _
Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
With Application
.ScreenUpdating = False
.DisplayAlerts = False
' End With
On Error Resume Next '<< a folder exists
MkDir MyFilePath '<< create a folder
For N = 1 To Sheets.Count
Sheets(N).Activate
SheetName = ActiveSheet.Name
Cells.Copy
Workbooks.Add (xlWBATWorksheet)
With ActiveWorkbook
With .ActiveSheet
.Paste
.Name = SheetName
[A1].Select
End With
'save book in this folder
.SaveAs Filename:=MyFilePath _
& "" & SheetName & ".xlsx"
.Close SaveChanges:=True
End With
.CutCopyMode = False
Next
End With
Sheet1.Activate
End Sub


I have a workbook, that contains many sheets which have visible and hide ones. I only want to export each visible sheet to individual workbook. this current code above can do the export for all the sheet in the workbook but I have to delete them 1 by 1 after that. Hope that explains my situation.










share|improve this question


























    0














    Sub SaveShtsAsBook() 
    ‘Select all visible and hide sheet’
    Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
    MyFilePath$ = ActiveWorkbook.Path & "" & _
    Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    ' End With
    On Error Resume Next '<< a folder exists
    MkDir MyFilePath '<< create a folder
    For N = 1 To Sheets.Count
    Sheets(N).Activate
    SheetName = ActiveSheet.Name
    Cells.Copy
    Workbooks.Add (xlWBATWorksheet)
    With ActiveWorkbook
    With .ActiveSheet
    .Paste
    .Name = SheetName
    [A1].Select
    End With
    'save book in this folder
    .SaveAs Filename:=MyFilePath _
    & "" & SheetName & ".xlsx"
    .Close SaveChanges:=True
    End With
    .CutCopyMode = False
    Next
    End With
    Sheet1.Activate
    End Sub


    I have a workbook, that contains many sheets which have visible and hide ones. I only want to export each visible sheet to individual workbook. this current code above can do the export for all the sheet in the workbook but I have to delete them 1 by 1 after that. Hope that explains my situation.










    share|improve this question
























      0












      0








      0







      Sub SaveShtsAsBook() 
      ‘Select all visible and hide sheet’
      Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
      MyFilePath$ = ActiveWorkbook.Path & "" & _
      Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
      With Application
      .ScreenUpdating = False
      .DisplayAlerts = False
      ' End With
      On Error Resume Next '<< a folder exists
      MkDir MyFilePath '<< create a folder
      For N = 1 To Sheets.Count
      Sheets(N).Activate
      SheetName = ActiveSheet.Name
      Cells.Copy
      Workbooks.Add (xlWBATWorksheet)
      With ActiveWorkbook
      With .ActiveSheet
      .Paste
      .Name = SheetName
      [A1].Select
      End With
      'save book in this folder
      .SaveAs Filename:=MyFilePath _
      & "" & SheetName & ".xlsx"
      .Close SaveChanges:=True
      End With
      .CutCopyMode = False
      Next
      End With
      Sheet1.Activate
      End Sub


      I have a workbook, that contains many sheets which have visible and hide ones. I only want to export each visible sheet to individual workbook. this current code above can do the export for all the sheet in the workbook but I have to delete them 1 by 1 after that. Hope that explains my situation.










      share|improve this question













      Sub SaveShtsAsBook() 
      ‘Select all visible and hide sheet’
      Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
      MyFilePath$ = ActiveWorkbook.Path & "" & _
      Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
      With Application
      .ScreenUpdating = False
      .DisplayAlerts = False
      ' End With
      On Error Resume Next '<< a folder exists
      MkDir MyFilePath '<< create a folder
      For N = 1 To Sheets.Count
      Sheets(N).Activate
      SheetName = ActiveSheet.Name
      Cells.Copy
      Workbooks.Add (xlWBATWorksheet)
      With ActiveWorkbook
      With .ActiveSheet
      .Paste
      .Name = SheetName
      [A1].Select
      End With
      'save book in this folder
      .SaveAs Filename:=MyFilePath _
      & "" & SheetName & ".xlsx"
      .Close SaveChanges:=True
      End With
      .CutCopyMode = False
      Next
      End With
      Sheet1.Activate
      End Sub


      I have a workbook, that contains many sheets which have visible and hide ones. I only want to export each visible sheet to individual workbook. this current code above can do the export for all the sheet in the workbook but I have to delete them 1 by 1 after that. Hope that explains my situation.







      excel vba show-hide visible






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 6:29









      chee seng ngchee seng ng

      248




      248






















          1 Answer
          1






          active

          oldest

          votes


















          0














          All you need to add to your code to exclude hidden sheets is a simple If..Then statement to check whether the Worksheet.Visible property is True or False.



          If Not yourWorsheet.Visible Then... ... then you skip that worksheet.




          The following procedure is a simpler overall approach to what you're trying to accomplish...



          Export Visible worksheets to their own workbooks:



          The worksheet.Copy method will create a new workbook if neither Before nor After are specified.



          Sub saveVisibleSheetsAsXLSM() 'saves all visible sheets as new xlsx files
          Const exportPath = "x:yourDestinationPath"
          Dim ws As Worksheet, wbNew As Workbook
          For Each ws In ThisWorkbook.Sheets 'for each worksheet
          If ws.Visible Then 'if it's visible:
          Debug.Print "Exporting: " & ws.Name
          ws.Copy '(if no params specified, COPY creates + activates a new wb)
          Set wbNew = Application.ActiveWorkbook 'get new wb object
          wbNew.SaveAs exportPath & ws.Name & ".xlsm", 52 'save new wb
          wbNew.Close 'close new wb
          Set wbNew = Nothing 'cleanup
          End If
          Next ws
          Set ws = Nothing 'clean up
          End Sub




          Worksheet.Copy Remarks:



          If you don't specify either Before or After, Microsoft Excel creates a new workbook that contains the copied sheet object that contains the copied Worksheet object. The newly created workbook holds the Application.ActiveWorkbook Property (Excel) property and contains a single worksheet. The single worksheet retains the Worksheet.Name Property (Excel) and Worksheet.CodeName Property (Excel) properties of the source worksheet. If the copied worksheet held a worksheet code sheet in a VBA project, that is also carried into the new workbook.



          An array selection of multiple worksheets can be copied to a new blank Workbook Object (Excel) object in a similar manner.



          (Source: Documentation)







          share|improve this answer






















          • Can I check if I want to infuse the If then statement to my existing code. How should I go about doing it?
            – chee seng ng
            Nov 20 '18 at 9:41










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275056%2fvba-export-only-visible-sheet-to-individual-workbook%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          All you need to add to your code to exclude hidden sheets is a simple If..Then statement to check whether the Worksheet.Visible property is True or False.



          If Not yourWorsheet.Visible Then... ... then you skip that worksheet.




          The following procedure is a simpler overall approach to what you're trying to accomplish...



          Export Visible worksheets to their own workbooks:



          The worksheet.Copy method will create a new workbook if neither Before nor After are specified.



          Sub saveVisibleSheetsAsXLSM() 'saves all visible sheets as new xlsx files
          Const exportPath = "x:yourDestinationPath"
          Dim ws As Worksheet, wbNew As Workbook
          For Each ws In ThisWorkbook.Sheets 'for each worksheet
          If ws.Visible Then 'if it's visible:
          Debug.Print "Exporting: " & ws.Name
          ws.Copy '(if no params specified, COPY creates + activates a new wb)
          Set wbNew = Application.ActiveWorkbook 'get new wb object
          wbNew.SaveAs exportPath & ws.Name & ".xlsm", 52 'save new wb
          wbNew.Close 'close new wb
          Set wbNew = Nothing 'cleanup
          End If
          Next ws
          Set ws = Nothing 'clean up
          End Sub




          Worksheet.Copy Remarks:



          If you don't specify either Before or After, Microsoft Excel creates a new workbook that contains the copied sheet object that contains the copied Worksheet object. The newly created workbook holds the Application.ActiveWorkbook Property (Excel) property and contains a single worksheet. The single worksheet retains the Worksheet.Name Property (Excel) and Worksheet.CodeName Property (Excel) properties of the source worksheet. If the copied worksheet held a worksheet code sheet in a VBA project, that is also carried into the new workbook.



          An array selection of multiple worksheets can be copied to a new blank Workbook Object (Excel) object in a similar manner.



          (Source: Documentation)







          share|improve this answer






















          • Can I check if I want to infuse the If then statement to my existing code. How should I go about doing it?
            – chee seng ng
            Nov 20 '18 at 9:41















          0














          All you need to add to your code to exclude hidden sheets is a simple If..Then statement to check whether the Worksheet.Visible property is True or False.



          If Not yourWorsheet.Visible Then... ... then you skip that worksheet.




          The following procedure is a simpler overall approach to what you're trying to accomplish...



          Export Visible worksheets to their own workbooks:



          The worksheet.Copy method will create a new workbook if neither Before nor After are specified.



          Sub saveVisibleSheetsAsXLSM() 'saves all visible sheets as new xlsx files
          Const exportPath = "x:yourDestinationPath"
          Dim ws As Worksheet, wbNew As Workbook
          For Each ws In ThisWorkbook.Sheets 'for each worksheet
          If ws.Visible Then 'if it's visible:
          Debug.Print "Exporting: " & ws.Name
          ws.Copy '(if no params specified, COPY creates + activates a new wb)
          Set wbNew = Application.ActiveWorkbook 'get new wb object
          wbNew.SaveAs exportPath & ws.Name & ".xlsm", 52 'save new wb
          wbNew.Close 'close new wb
          Set wbNew = Nothing 'cleanup
          End If
          Next ws
          Set ws = Nothing 'clean up
          End Sub




          Worksheet.Copy Remarks:



          If you don't specify either Before or After, Microsoft Excel creates a new workbook that contains the copied sheet object that contains the copied Worksheet object. The newly created workbook holds the Application.ActiveWorkbook Property (Excel) property and contains a single worksheet. The single worksheet retains the Worksheet.Name Property (Excel) and Worksheet.CodeName Property (Excel) properties of the source worksheet. If the copied worksheet held a worksheet code sheet in a VBA project, that is also carried into the new workbook.



          An array selection of multiple worksheets can be copied to a new blank Workbook Object (Excel) object in a similar manner.



          (Source: Documentation)







          share|improve this answer






















          • Can I check if I want to infuse the If then statement to my existing code. How should I go about doing it?
            – chee seng ng
            Nov 20 '18 at 9:41













          0












          0








          0






          All you need to add to your code to exclude hidden sheets is a simple If..Then statement to check whether the Worksheet.Visible property is True or False.



          If Not yourWorsheet.Visible Then... ... then you skip that worksheet.




          The following procedure is a simpler overall approach to what you're trying to accomplish...



          Export Visible worksheets to their own workbooks:



          The worksheet.Copy method will create a new workbook if neither Before nor After are specified.



          Sub saveVisibleSheetsAsXLSM() 'saves all visible sheets as new xlsx files
          Const exportPath = "x:yourDestinationPath"
          Dim ws As Worksheet, wbNew As Workbook
          For Each ws In ThisWorkbook.Sheets 'for each worksheet
          If ws.Visible Then 'if it's visible:
          Debug.Print "Exporting: " & ws.Name
          ws.Copy '(if no params specified, COPY creates + activates a new wb)
          Set wbNew = Application.ActiveWorkbook 'get new wb object
          wbNew.SaveAs exportPath & ws.Name & ".xlsm", 52 'save new wb
          wbNew.Close 'close new wb
          Set wbNew = Nothing 'cleanup
          End If
          Next ws
          Set ws = Nothing 'clean up
          End Sub




          Worksheet.Copy Remarks:



          If you don't specify either Before or After, Microsoft Excel creates a new workbook that contains the copied sheet object that contains the copied Worksheet object. The newly created workbook holds the Application.ActiveWorkbook Property (Excel) property and contains a single worksheet. The single worksheet retains the Worksheet.Name Property (Excel) and Worksheet.CodeName Property (Excel) properties of the source worksheet. If the copied worksheet held a worksheet code sheet in a VBA project, that is also carried into the new workbook.



          An array selection of multiple worksheets can be copied to a new blank Workbook Object (Excel) object in a similar manner.



          (Source: Documentation)







          share|improve this answer














          All you need to add to your code to exclude hidden sheets is a simple If..Then statement to check whether the Worksheet.Visible property is True or False.



          If Not yourWorsheet.Visible Then... ... then you skip that worksheet.




          The following procedure is a simpler overall approach to what you're trying to accomplish...



          Export Visible worksheets to their own workbooks:



          The worksheet.Copy method will create a new workbook if neither Before nor After are specified.



          Sub saveVisibleSheetsAsXLSM() 'saves all visible sheets as new xlsx files
          Const exportPath = "x:yourDestinationPath"
          Dim ws As Worksheet, wbNew As Workbook
          For Each ws In ThisWorkbook.Sheets 'for each worksheet
          If ws.Visible Then 'if it's visible:
          Debug.Print "Exporting: " & ws.Name
          ws.Copy '(if no params specified, COPY creates + activates a new wb)
          Set wbNew = Application.ActiveWorkbook 'get new wb object
          wbNew.SaveAs exportPath & ws.Name & ".xlsm", 52 'save new wb
          wbNew.Close 'close new wb
          Set wbNew = Nothing 'cleanup
          End If
          Next ws
          Set ws = Nothing 'clean up
          End Sub




          Worksheet.Copy Remarks:



          If you don't specify either Before or After, Microsoft Excel creates a new workbook that contains the copied sheet object that contains the copied Worksheet object. The newly created workbook holds the Application.ActiveWorkbook Property (Excel) property and contains a single worksheet. The single worksheet retains the Worksheet.Name Property (Excel) and Worksheet.CodeName Property (Excel) properties of the source worksheet. If the copied worksheet held a worksheet code sheet in a VBA project, that is also carried into the new workbook.



          An array selection of multiple worksheets can be copied to a new blank Workbook Object (Excel) object in a similar manner.



          (Source: Documentation)








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 7:24

























          answered Nov 13 '18 at 7:08









          ashleedawgashleedawg

          12.7k42149




          12.7k42149











          • Can I check if I want to infuse the If then statement to my existing code. How should I go about doing it?
            – chee seng ng
            Nov 20 '18 at 9:41
















          • Can I check if I want to infuse the If then statement to my existing code. How should I go about doing it?
            – chee seng ng
            Nov 20 '18 at 9:41















          Can I check if I want to infuse the If then statement to my existing code. How should I go about doing it?
          – chee seng ng
          Nov 20 '18 at 9:41




          Can I check if I want to infuse the If then statement to my existing code. How should I go about doing it?
          – chee seng ng
          Nov 20 '18 at 9:41

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275056%2fvba-export-only-visible-sheet-to-individual-workbook%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線