+ Reply to Thread
Results 1 to 2 of 2

Alteration needed: Sort sheets with cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    03-24-2013
    Location
    Gloucester
    MS-Off Ver
    Excel 2013
    Posts
    20

    Alteration needed: Sort sheets with cell value

    Hi Guys,

    Below code is good, however i would like it to only copy sheets with a specific value in a cell. So for instants if there is the word "Yes" in a cell then copy that sheet. so if i have 32 sheets and only 4 of then contain the word "Yes" in cell "A1" then those 4 sheets get put into a new workbook and saved to a path. Could someone please advise.

    Thanks,

    Alan

    Sub Copy_Every_Sheet_To_New_Workbook()
    'Working in 97-2013
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim Sourcewb As Workbook
        Dim Destwb As Workbook
        Dim sh As Worksheet
        Dim DateString As String
        Dim FolderName As String
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
            .Calculation = xlCalculationManual
        End With
    
        'Copy every sheet from the workbook with this macro
        Set Sourcewb = ThisWorkbook
    
        'Create new folder to save the new files in
        DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
        FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
        MkDir FolderName
    
        'Copy every visible sheet to a new workbook
        For Each sh In Sourcewb.Worksheets
    
            'If the sheet is visible then copy it to a new workbook
            If sh.Visible = -1 Then
                sh.Copy
    
                'Set Destwb to the new workbook
                Set Destwb = ActiveWorkbook
    
                'Determine the Excel version and file extension/format
                With Destwb
                    If Val(Application.Version) < 12 Then
                        'You use Excel 97-2003
                        FileExtStr = ".xls": FileFormatNum = -4143
                    Else
                        'You use Excel 2007-2013
                        If Sourcewb.Name = .Name Then
                            MsgBox "Your answer is NO in the security dialog"
                            GoTo GoToNextSheet
                        Else
                            Select Case Sourcewb.FileFormat
                            Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                            Case 52:
                                If .HasVBProject Then
                                    FileExtStr = ".xlsm": FileFormatNum = 52
                                Else
                                    FileExtStr = ".xlsx": FileFormatNum = 51
                                End If
                            Case 56: FileExtStr = ".xls": FileFormatNum = 56
                            Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                            End Select
                        End If
                    End If
                End With
    
                'Change all cells in the worksheet to values if you want
                If Destwb.Sheets(1).ProtectContents = False Then
                    With Destwb.Sheets(1).UsedRange
                        .Cells.Copy
                        .Cells.PasteSpecial xlPasteValues
                        .Cells(1).Select
                    End With
                    Application.CutCopyMode = False
                End If
    
    
                'Save the new workbook and close it
                With Destwb
                    .SaveAs FolderName _
                          & "\" & Destwb.Sheets(1).Name & FileExtStr, _
                            FileFormat:=FileFormatNum
                    .Close False
                End With
    
            End If
    GoToNextSheet:
        Next sh
    
        MsgBox "You can find the files in " & FolderName
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
        End With
    End Sub

  2. #2
    Registered User
    Join Date
    03-24-2013
    Location
    Gloucester
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: Alteration needed: Copy sheets with cell value

    Change of Title to Match need.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1