Advertisement
Nihad

VBA Code: Copy All (Visible) Tabs to Individual Files PDF

Apr 22nd, 2024 (edited)
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.28 KB | Source Code | 0 0
  1. Sub CopyTabsToIndividualFiles()
  2. 'PURPOSE: Copy all visible tabs to individual files
  3. 'SOURCE: www.TheSpreadsheetGuru.com
  4.  
  5. Dim FolderPath As String
  6. Dim sht As Worksheet
  7.  
  8. 'Determine Folder Path
  9.   FolderPath = Application.ActiveWorkbook.Path
  10.  
  11. 'Optimize Code (Turn off)
  12.   Application.ScreenUpdating = False
  13.   Application.DisplayAlerts = False
  14.  
  15. 'Loop through each sheet in ActiveWorkbook
  16.   For Each sht In ThisWorkbook.Worksheets
  17.    
  18.     'Ensure sheet is visible so we can copy it
  19.       If sht.Visible = xlSheetVisible Then
  20.        
  21.         'Copy Sheet to new workbook
  22.           sht.Copy
  23.           DoEvents
  24.    
  25.         'Save tab as an Excel File (use Sheet's name)
  26.           Application.ActiveWorkbook.SaveAs Filename:=FolderPath & "\" & sht.Name & ".xlsx"
  27.        
  28.         'Save tab as a PDF File (use Sheet's name)
  29.           Application.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
  30.             Filename:=FolderPath & "\" & sht.Name & ".pdf"
  31.        
  32.         'Close File
  33.           Application.ActiveWorkbook.Close False
  34.    
  35.       End If
  36.    
  37.   Next
  38.  
  39. 'Optimize Code (Turn back on)
  40.   Application.DisplayAlerts = True
  41.   Application.ScreenUpdating = True
  42.  
  43. 'Notify user all copies have been made
  44.   MsgBox "All sheets have been saved to individual files"
  45.  
  46. End Sub
Tags: vba
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement