Results 1 to 8 of 8

Thread: Macro to clear data based on color fill

  1. #1
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13

    Macro to clear data based on color fill

    I would like code to clear data on all sheets from row 5 onwards where the data has been colored using the color fill, except formulas-see sample data attached

    Your assistance is most appreciated.

    I have also posted on Mr Excel.com-see link below


    Macro to clear data
    Attached Files Attached Files

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    try this.

    Code:
    Sub kTest()
        
        Dim Sht     As Worksheet
        Dim LastRow As Long
        Dim LastCol As Long
        Dim rngAll  As Range
        Dim r       As Range
        Dim c       As Range
        
        On Error Resume Next
        For Each Sht In Worksheets
            With Sht
                If .UsedRange.Cells.Count > 1 Then
                    LastRow = .Cells.Find("*", .Cells(1), , 2, 1, 2).Row
                    LastCol = .Cells.Find("*", .Cells(1), , 2, 2, 2).Column
                    Set rngAll = .Range(.Cells(5, 1), .Cells(LastRow, LastCol))
                    For Each r In rngAll.SpecialCells(2, 23).Areas
                        For Each c In r.Cells
                            If c.Interior.ColorIndex <> -4142 Then
                                c.Clear 'if you want to cleat the both the formats and data
                                'c.ClearContents'removes only data
                            End If
                        Next
                    Next
                End If
            End With
        Next
                    
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13
    Thanks for the help, much appreciated

  4. #4
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13
    Hi admin

    I would like to amend the code so that sheet 'main" is ignored when the colored data are cleared

    Your assistance is most appreciated

  5. #5
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Try to modify yourself and learn VBA !

    here is the way to avoid sheet Main.

    Code:
    Sub kTest()
        
        Dim Sht     As Worksheet
        Dim LastRow As Long
        Dim LastCol As Long
        Dim rngAll  As Range
        Dim r       As Range
        Dim c       As Range
        
        On Error Resume Next
        For Each Sht In Worksheets
            If LCase$(Sht.Name) <> "main" Then
                With Sht
                    If .UsedRange.Cells.Count > 1 Then
                        LastRow = .Cells.Find("*", .Cells(1), , 2, 1, 2).Row
                        LastCol = .Cells.Find("*", .Cells(1), , 2, 2, 2).Column
                        Set rngAll = .Range(.Cells(5, 1), .Cells(LastRow, LastCol))
                        For Each r In rngAll.SpecialCells(2, 23).Areas
                            For Each c In r.Cells
                                If c.Interior.ColorIndex <> -4142 Then
                                    c.Clear 'if you want to cleat the both the formats and data
                                    'c.ClearContents'removes only data
                                End If
                            Next
                        Next
                    End If
                End With
            End If
        Next
                    
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  6. #6
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13
    Hi Admin

    Thanks for the help. Are there any good tutorials or books on VBA that you can recommend to novices?

  7. #7
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Thanks for the feedback.

    Have a look at http://www.excelfox.com/forum/f13/le...ful-links-144/
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  8. #8
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13
    Thanks for the links. i'm sure that I will find these useful

Similar Threads

  1. Replies: 2
    Last Post: 05-30-2013, 07:28 PM
  2. Replies: 9
    Last Post: 12-04-2012, 09:45 PM
  3. Macro To Clear Certain Data Across Many Sheets
    By Howardc in forum Excel Help
    Replies: 2
    Last Post: 12-04-2012, 09:10 AM
  4. Macro to check values based on certain text
    By Howardc in forum Excel Help
    Replies: 25
    Last Post: 11-05-2012, 09:03 PM
  5. Replies: 4
    Last Post: 07-27-2012, 08:43 AM

Posting Permissions

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