Macro does not clear the contents!

 

I have a worksheet which has an income and expense analysis for 12 months. I would like to re-use it by keeping the formula and format intact, but clear the contents of each cell. So, if a cell does not have a formula, but has a text or number, it should be clear after I run the macro.

I came up with a simple macro, but did not seem to work, so I have posted the question on the code forum site. I have now two macros in which one of the macro does not seem to have a syntax error, but does not do the job, i.e. it loops through each sheet, but does not clear the contents. The other macro has a syntax error.

Could someone please spot what is wrong with these two codes?

Cheers

 

Sub clearcontents()

    Dim Rng As Range, oCell As Range

    Set Rng = Range("A2:A100")

    For sh = 1 To Worksheets.Count '+for each sheet

        Sheets(sh).Activate '+

        For Each oCell In Rng

            If oCell.HasFormula Then

                Exit Sub

            Else

                oCell.clearcontents

            End If

        Next

    Next sh '+

End Sub

 

Sub snb()

  For Each sh In Sheets

    sh.Range("A2:A100").SpecialCells(2).clearcontents          ‘ a line which shows an error

  Next sh

End Sub

Comments
There are 9 comments. Login or register to view them.

Try this

leestevens |

Couldn't you just...

George Attazder |
George Attazder's picture

Macro does not clear the contents!

Cantona1 |

Is that not exiting

ACDWebb |
ACDWebb's picture

Macro does not clear the contents!

Cantona1 |

Blush. I REALLY wouldn't go that far!!

ACDWebb |
ACDWebb's picture

Try this

George Attazder |
George Attazder's picture

Macro does not clear the contents!

Cantona1 |

Macro does not clear the contents!

Cantona1 |