• Clear data except Formulas & any coloured items

    Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Clear data except Formulas & any coloured items

    Author
    Topic
    #507755

    I have written code to clear data except formulas from all sheet starting from sheet 3 and from A15 to the last row in Col M

    I want to amend the formula so that any coloured cells using the fill colour are also not cleared

    Code:
     Sub Clear_Data
     Dim Sh As Integer
        Dim LR As Long
        
        For Sh = 3 To Worksheets.Count
            With Worksheets(Sh)
                LR = .Cells(.Rows.Count, "A").End(xlUp).Row
                If LR < 15 Then LR = 15
     On Error Resume Next
                .Range("A15:M" & LR).SpecialCells(xlCellTypeConstants).ClearContents
                On Error GoTo 0
                        End With
        Next Sh
    End Sub 

    It would be appreciated if someone could kindly assist me

    Viewing 2 reply threads
    Author
    Replies
    • #1586734

      Hi Howard

      ..are you using any conditional formatting that sets cell fill colour?
      ..or are the cells ‘manually’ coloured??

      zeddy

    • #1586748

      I think you’ll need a loop like this:

      Code:
      Sub Clear_Data()
          Dim Sh                    As Integer
          Dim LR                    As Long
          Dim cell                  As Range
      
          Application.ScreenUpdating = False
          For Sh = 3 To Worksheets.Count
              With Worksheets(Sh)
                  LR = .Cells(.Rows.Count, "A").End(xlUp).Row
                  If LR < 15 Then LR = 15
                  On Error Resume Next
                  For Each cell In .Range("A15:M" & LR).SpecialCells(xlCellTypeConstants).Cells
                      If cell.DisplayFormat.Interior.ColorIndex = xlColorIndexNone Then cell.ClearContents
                  Next cell
                  On Error GoTo 0
              End With
          Next Sh
          Application.ScreenUpdating = True
      End Sub
      
      • #1586752

        Hi Howard

        ..Rory has a great solution.

        For speedup, I would also include turning events and calcs OFF at the start, and turning them back ON at the end.

        zeddy

        • #1586764

          Hi Zeddy

          Thanks for the tip. It made a huge difference

          • #1586771

            Hi Howard

            I find you can also get a macro to run much faster if the VBA editor is closed when you run the macro e.g. assign the macro to a button and run it from there.

            zeddy

      • #1586762

        Thanks for the help Rory. Going to turn events and calc off at the start and then back on as it is very slow

    • #1586802

      hi Zeddy

      Thanks for the tip

      That’s something I did not know, although I do assign macro’s to a button in many instances

      Howard

    Viewing 2 reply threads
    Reply To: Clear data except Formulas & any coloured items

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: