Home > Software design >  Extract multiple tabs based on tab color into multiple flat files
Extract multiple tabs based on tab color into multiple flat files

Time:11-23

I am trying to export tabs in my excel workbook which are in green color into csv files. Any pointers will be much appreciated.

CodePudding user response:

Export to CSV

Basic

Sub ExportToCSV()
    
    Const DST_PATH As String = "C:\Test"
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code

    Application.ScreenUpdating = False
    
    Dim sws As Worksheet
    Dim dwb As Workbook
    Dim dFilePath As String
    
    For Each sws In wb.Worksheets
        If sws.Tab.Color = vbGreen Then
            sws.Copy ' creates a new single-worksheet workbook
            Set dwb = Workbooks(Workbooks.Count)
            dFilePath = DST_PATH & Application.PathSeparator & sws.Name & ".csv"
            Application.DisplayAlerts = False ' overwrite without confirmation
                dwb.SaveAs Filename:=dFilePath, FileFormat:=xlCSV ' xlCSVUTF8
            Application.DisplayAlerts = True
            dwb.Close SaveChanges:=False
        End If
    Next sws

    Application.ScreenUpdating = True
    
    MsgBox "Worksheets exported.", vbInformation

End Sub
  • Related