+ Reply to Thread
Results 1 to 5 of 5

Keep sheets based on color and copy paste as values. Delete the remaining ones

Hybrid View

  1. #1
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Keep sheets based on color and copy paste as values. Delete the remaining ones

    I have the following macro that should keep the tabs that have a specific color, turn them to values and delete all the other. The color part works in another macro that I use to PDF the sheets but not here. The second For works as well. But put together, the workbook freezes and nothing happens. Can someone point me in a direction about it?
    Option Explicit
    
    Sub test()
    MsgBox (Sheets("Sheet1").Tab.ColorIndex)
    
    
    End Sub
    
    Sub ExportAsPDFSTN()
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False
        Dim strName As String, strPathFile As String, wsNames() As String
        Dim ws As Worksheet
        Dim wsColor() As Integer, ind As Integer
        Dim Matched As Boolean
        Dim wshtName As Variant 
          ReDim wsNames(0)
             ReDim wsColor(0)
                wsNames(0) = ThisWorkbook.Sheets("Cover").Name
    
        strName = ThisWorkbook.Sheets("Admin").Range("A5").Value
                 Application.CalculateUntilAsyncQueriesDone
            Do While Application.CalculationState <> xlDone
              DoEvents
                  Loop
          For Each ws In ThisWorkbook.Sheets
        If ws.Tab.ColorIndex = wsColor(0) And ws.Visible = xlSheetVisible Then
          ReDim Preserve wsNames(UBound(wsNames) + 1)
          ReDim Preserve wsColor(UBound(wsColor) + 1)
          wsNames(UBound(wsNames)) = ws.Name
          wsColor(UBound(wsColor)) = ws.Tab.ColorIndex
        End If
          Next ws
    
      For Each ws In ThisWorkbook.Worksheets
            Matched = False
            For Each wshtName In wsNames
                If wshtName = ws.Name Then
                    Matched = True
                    With ws.UsedRange
                .Cells.Copy
                .Cells.PasteSpecial xlPasteValues
            End With
            Application.CutCopyMode = False
                End If
            Next
            If Not Matched Then
                ws.Delete
            End If
        Next ws
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.AskToUpdateLinks = True
     
     ThisWorkbook.Sheets("Admin").Activate
     ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\Distributed\" & strName
     
    End Sub
    Click the * to say thanks.

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Keep sheets based on color and copy paste as values. Delete the remaining ones

    wsColor(0) is never set so will probably equal zero which won't match any sheets. Perhaps you need somewhere:

    wsColor(0) = ThisWorkbook.Sheets("Cover").Tab.ColorIndex
    Probably after the line that sets wsNames(0).

    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Keep sheets based on color and copy paste as values. Delete the remaining ones

    Oh, I cleaned the macro and removed that line by mistake. I already have it my actual file set, but unfortunately it still not works.

  4. #4
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Keep sheets based on color and copy paste as values. Delete the remaining ones

    Can you redact the data and attach a copy of the file? Would probably be trivial to diagnose then but almost impossible without having a copy of the file.

    WBD

  5. #5
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Keep sheets based on color and copy paste as values. Delete the remaining ones

    In case someone will find this thread, here is a solution. Not sure why I overcomplicated the problem.
    Sub ExportAsPDFSTN()
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False
        Dim strPath As String, strName As String, strPathFile As String, wsNames() As String
        Dim ws As Worksheet
        Dim wsColor() As Integer
          ReDim wsNames(0)
            ReDim wsColor(0)
              wsNames(0) = ThisWorkbook.Sheets("Cover").Name
              wsColor(0) = ThisWorkbook.Sheets("Cover").Tab.ColorIndex
    
        strName = ThisWorkbook.Sheets("Admin").Range("A5").Value & " " & ThisWorkbook.Sheets("Cover").Range("B41")
         Application.CalculateUntilAsyncQueriesDone
    Do While Application.CalculationState <> xlDone
       DoEvents
    Loop
         For Each ws In ThisWorkbook.Sheets
        If ws.Tab.ColorIndex = wsColor(0) Then
            With ws.UsedRange
                .Cells.Copy
                .Cells.PasteSpecial xlPasteValues
            End With
            ws.Visible = xlSheetVisible
            Else
            ws.Delete
        End If
      Next ws
      
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.AskToUpdateLinks = True
     
     ThisWorkbook.Sheets("Admin").Activate
     ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\Distributed\" & strName
     
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copy & Paste Cell Values based on conditions to separate sheets from primary sheet
    By spyac in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-05-2018, 08:04 PM
  2. [SOLVED] Create new sheets and copy/paste data based on values in a column or columns
    By quintans1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2015, 12:15 PM
  3. Replies: 1
    Last Post: 10-05-2014, 12:28 PM
  4. Compare two sheets,copy and paste unique rows based on values in 2 columns
    By ooggiemobile in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2013, 03:58 AM
  5. [SOLVED] Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2005, 08:12 PM
  6. Replies: 0
    Last Post: 06-21-2005, 01:05 PM
  7. [SOLVED] Re: Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-21-2005, 01:05 PM

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