1

I am attempting to write code that will completely wipe cell contents in a workbook.

I'd like to have two similar Subs: One Sub to clear all contents and another Sub to clear all contents starting from 2nd row to the last row.

I am using following code but it's returning an error:

Sub Populate_RA_Sheet()
    '
    ' Populate_RA_Sheet Macro
    '
    RA_Sheet(2).Cells.ClearContents
    '
End Sub
Community
  • 1
  • 1
user2786962
  • 469
  • 5
  • 7
  • 13

1 Answers1

7

Let's say the name of the sheet is Sheet1 Then

One code clear all content

ThisWorkbook.Sheets("Sheet1").Cells.ClearContents

and other code clear all contents starting from 2nd row till the last row whatever it is.

You can find the last row and then clear the contents. But why would you want to do that? Simply Clear ALL rows after the 2nd Row?

With ThisWorkbook.Sheets("Sheet1")
    .Rows("2:" & .Rows.Count).ClearContents
End With

If you still want to clear all contents starting from 2nd row till the last row then see THIS on how to find the last row and once you get the last row then simply use this

With ThisWorkbook.Sheets("Sheet1")
    .Rows("2:" & LastRow).ClearContents
End With

Note:

  1. The above code will fail if say Cell A1:A2 is merged. So be careful of that :)
  2. This code will clear the cells but not delete any shapes/formatting. You will have to handle it separately.
Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250